I have extracted some query parameters and URI path parameters in request flow. I want to add these variables to the request payload.I have extracted using Extract Variable Policy. How to add these to my JSON request payload?
My Extract Variable Policy is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-GetStartDate"> <DisplayName>EV-GetPathType</DisplayName> <URIPath name="index" type="string"> <Pattern ignoreCase="true">/{index}</Pattern> </URIPath> <QueryParam name="type" type="string"> <Pattern ignoreCase="true">{type}</Pattern> </QueryParam> <VariablePrefix>queryinfo</VariablePrefix> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> </ExtractVariables>
I like to do this with a Javascript policy on the request flow. Please see below an example:
// read your flow variables var index = context.getVariable("queryinfo.index"); var type = context.getVariable("queryinfo.type"); // generate new JSON payload var newJSON = {'index': index, 'type': type}; // write request payload context.setVariable('request.content', JSON.stringify(newJSON));
But I need to add the flow variables to the input json payload.
If you would like to modify your existing json request, using javascript read in the request content as a json object and manipulate it
var requestJson = JSON.parse(context.getVariable('request.content')); var type = context.getVariable('queryinfo.type'); requestJson.type = type; context.setVariable('request.content', JSON.stringify(requestJson));