We are sending this request payload in body
Sample json:-
- {
- “employee”: {
- “name”: “sonoo”,
- “salary”: 56000,
- “married”: true
- }
- }
From above payload while “salary” place
- {
- “employee”: {
- “name”: “sonoo”,
- “income”: 56000,
- “married”: true
- }
- }
Backend service accepts only income pay load but i need to send request body as salary element
How to transform this payload in apigee before sending request
Can any one suggest me
a @Siddharth Barahalikar , @Dino-at-Google ,
Yes, you can do that kind of thing very simply; I recommend that you use a JS callout to perform that replacement. Here’s what the JS code would look like:
/* global context */
/* jslint esversion: 6 */
var c = JSON.parse(context.getVariable('request.content'));
c.salary = c.income;
delete c.income;
context.setVariable(JSON.stringify(c));
And the JS policy configuration is also simple:
<Javascript name='JS-1' timeLimit='200' >
<ResourceURL>jsc://modifyRequest.js</ResourceURL>
</Javascript>
On the doc page, you can learn how to add a JS policy to your API Proxy. There’s a quick screencast describing the process.
https://docs.apigee.com/api-platform/reference/policies/javascript-policy
1 Like
@Dino-at-GoogleThanks it’s working
1 Like
I love it when that happens.
Is this correct ,context.setVariable(JSON.stringify(c)); ?
I think it should be something context.setVariable(‘request’,JSON.stringify(c));
context.setVariable(‘request.content’, JSON.stringify(myMeaningfulVariableName))
You should probably use trace to confirm the content-length header is being generated correctly too