How to read variable from request payload in Extract variable policy and javascript?

Not applicable
 
Solved Solved
0 11 7,233
1 ACCEPTED SOLUTION

Not applicable

Hello @neha,

Following is an example of how to do it for a JSON payload:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract.Parameters">
    <DisplayName>Extract.Parameters</DisplayName>
    <Properties/>
    <JSONPayload>
    <Variable name="abcd">
        <JSONPath>$.abcd</JSONPath>
    </Variable>
    <Variable name="efgh">
        <JSONPath>$.efgh</JSONPath>
    </Variable>
</JSONPayload>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <Source clearPayload="false">request</Source>
</ExtractVariables>

But more information could be found here. Please go through this documentation and would would be able to explore many other options as well.

Hope this helps !

View solution in original post

11 REPLIES 11

Not applicable

Hello @neha,

Following is an example of how to do it for a JSON payload:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract.Parameters">
    <DisplayName>Extract.Parameters</DisplayName>
    <Properties/>
    <JSONPayload>
    <Variable name="abcd">
        <JSONPath>$.abcd</JSONPath>
    </Variable>
    <Variable name="efgh">
        <JSONPath>$.efgh</JSONPath>
    </Variable>
</JSONPayload>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <Source clearPayload="false">request</Source>
</ExtractVariables>

But more information could be found here. Please go through this documentation and would would be able to explore many other options as well.

Hope this helps !

Thanks Meghdeep Basu.

Can you please tell me how to get request payload in javascript?

In javascriot you could do it as follows:

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

And the simply access any parameter in the payload object as follows

payload.param

Hi,

I tried this but it is giving Error: "Execution of JavaScript-1 failed with error: Javascript runtime error: \"SyntaxError: Empty JSON string (Script_1_js#1). at line 1 \""

PFB my request payload:

{

"Url" : "abc.xyz.com",

"data" : { }

}

Below is my JavaScript in APIGEE:

var getPayload = JSON.parse(context.getVariable("request.content"));
print("getPayload::::" +getPayload);
var str = context.getVariable("getPayload.Url");
var n = false;
if(str !== null){
 var n = str.includes("xyz");
}
if(n)
print("n::::" +  n); //here I want to apply OAUTH Policy
else{
print("n::::"+n);
}

Can you please look into this if I am missing anything?

Thanks.

@neha,

For some reason, the "request.content" is null. Can you please put a null check ? If it does not work, can you please try context.getVariable("message.content")?

Could you please add another print statement to see what is the content of "request.content"?

print(context.getVariable("request.content"));

Because from the error you are getting, it looks to me at that stage in the flow the request body is empty, and not the one that you posted in your comment.

Hi,

I added print statement , it is giving empty for request.content and message.content but I am hitting my proxy with above payload

Thats strange. I hope the content type header is application/json. Can you please share the trace ?

Hi,

Now I am getting payload but not able to read parameter from that

var getPayload = context.getVariable('request.content'); 
print("getPayload::::" +getPayload); // it is printing 'abc.xyz.com' 
var str = context.getVariable("getPayload.Url");
print("str::::" +str); //it is null

Hi,

Thank you so much Meghdeep Basu and Miren for your help.

I am able to get request payload and it's parameter now.

var getPayload = JSON.parse(context.getVariable('request.content'));

print("getPayload::::" +getPayload);

var str = getPayload.Url;

print("str::::" +str);

Hi @Meghdeep Basu

How to validate the payload value of assign message policy using javascript