Replace request payload json parameter

We are sending this request payload in body

Sample json:-

  1. {
  2. "employee": {
  3. "name": "sonoo",
  4. "salary": 56000,
  5. "married": true
  6. }
  7. }

From above payload while "salary" place

  1. {
  2. "employee": {
  3. "name": "sonoo",
  4. "income": 56000,
  5. "married": true
  6. }
  7. }

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 ,

Solved Solved
0 5 1,251
1 ACCEPTED SOLUTION

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

View solution in original post

5 REPLIES 5

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

@Dino-at-GoogleThanks it's working

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