Adding the extra field to the request payload is not working

Not applicable
var payload = JSON.parse(context.getVariable("request.content"));
var clientId = context.getVariable(clientId);
payload.clientId = clientId;
context.setVariable("request.content", JSON.stringify(payload));

I want to add another field in the JSON request payload.

i referred to some other answers in the blog and followed the examples I found there. But it's not working for me.

In trace its not adding the clientid.

Please guide where is the mistake in the code.

0 7 947
7 REPLIES 7

If your cut/paste is accurate, you are missing quotes surrounding the clientId in the 2nd line. And also you need to use all lowercase.

Generalizing, it might look like this:

var stringVariableName = 'clientid';
var payload = JSON.parse(context.getVariable("request.content"));
var value = context.getVariable(stringVariableName);
payload[stringVariableName] = value;
context.setVariable("request.content", JSON.stringify(payload));

I'm pretty sure this will solve your problem.

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

var externalId=context.getVariable('externalId');

print("externalId"+externalId);

payload[externalId] = externalId;

context.setVariable("request.content", JSON.stringify(payload)); print("payload:"+JSON.stringify(payload));

i have changed the code but still its not adding the paramter to payload

Are you trying to manipulate the target request? Where is the policy being attached?

attachaed this policy at target endpoint request flow

What is 'externalid' ?

The code might work, and it might not, depending on the presence of that context variable.

Can you show a trace screen with the output of the print statement, and the JS policy highlighted?

externalid is a variable which i have retrieved from path param in extract message policy that i am retrieve with context.getvariable.

this external id i cant able to add to request payload...

is it issue of placing the policy in target endpoint request flow.

Yes, if you are making changes to the target request, you have to attach it in the taget endpoint preflow/flow