Adding a extra field with request payload?

Not applicable

Hi, I want to add one extra field to my request payload.

If my payload is:

{
"firstName":"ABC",
"lastName":"XXX"
}

I want to have my payload as like this:

{
"firstName":"ABC",
"lastName":"XXX",
"id":1
}

But, to do this, I want to extract all the variables and then I assign it to my payload.Instead of doing this, is there a easier way?

0 4 3,103
4 REPLIES 4

If it is XML, You can use XSLT Apigee Policy.

If it is JSON, You need to use either Javascript or Java Policy to add new parameter as simple as ,

var payload = JSON.parse(context.getVariable("response.content"));
payload.id = 1;
context.setVariable("response.content", payload);

Hope it helps.

-------------------------------

Anil Sagar

5997-screen-shot-2017-11-23-at-75916-pm.png Learn Apigee Concepts in 4 Minutes HandsOn

its var payload = JSON.parse(context.getVariable("request.content"));

request.content

Hi,

I Made code like below:

var payload = JSON.parse(context.getVariable("request.content"));

payload.apiKey =201001;

context.setVariable("request.content", payload);

But final output became like below it's returning. Can you please check where i am making mistake here?

request.content [object Object]

Are you using

print(request.content);

in a Javascript policy for checking the final output?

If so, you'll need to replace it with

print(JSON.stringify(request.content));