Not able to get body content in javascript policy

Not applicable

Hi,

I am sending body content as json format:

{

"firstname":"vinay" ,

"lastname":"soni"

}

now i have to receive this body content as a string in my javascript, so i can append it as query parameter of my target url like:

?info='{"firstname":"vinay" ,"lastname":"soni"}'

i have tried multiple things, but didn't get it as expected.

Need Help ??

0 12 5,367
12 REPLIES 12

Dear @Vinay Soni ,

You can able to do same using javascript.

Try below code,

 var getPayload = context.getVariable("request.content");
 context.setVariable("request.queryparam.info", getPayload);

I don't think it's a good practice to send json as query param. Your url will be encoded since it contains special characters.

Cheers,

Anil Sagar

1207-capture.png

1202-capture.png

Hi @Anil Sagar,

here's my snapshot of postman. i am getting error when i am trying to use "request.content" in my javascript. i tried it earlier.

and i don't have requirement to extract any values from json. i just need the whole json content as a string as mentioned above.

Dear @Vinay Soni ,

Your Attachment is missing. Please embed it using image option in editor.

Please find screenshots below for your reference from my post man call. Make sure you set header with content-type application/json

1203-screen-shot-2015-09-19-at-93557-pm.png

1204-screen-shot-2015-09-19-at-93604-pm.png

Dear @Vinay Soni ,

Please post trace details & error details. When you say an error, without details it's difficult to understand. Please provide details to better understand same.

@Anil Sagar

tracing screenshot....

you can see the body content is coming here but i am not able to get it through "request.content"...

i have also attached postman snapshot

1210-capture1.png

@Vinay Soni , I don't see any error in trace. Did you use the proxy i have attached ? Can you post your javascript code & post man call too.. It should work out of the box.

@Anil S agar

Hi Anil, I tried your proxy and found the issue.

when you add the javascript policy in the target request instead of proxy request.

and modify your javascript like this :

 var getPayload = context.getVariable("request.content");
var targetUrl = "http://google.com?visitorInfo="+getPayload;
 context.setVariable("target.url",targetUrl);

it will fail and that's the same issue i am geting in my proxy.

here's the response:

{
    "fault": {
        "faultstring": "Execution of JavaScript-1 failed on line 6 with error: Unresolved variable : \n  \"id\"",
        "detail": {
            "errorcode": "steps.javascript.ScriptExecutionFailedLineNumber"
        }
    }
}

can you check ??

@Vinay Soni ,It does work for me in target endpoint. See attached proxy in updated answer.. petstore-rev1-2015-09-19-1.zip(2.0 kB)

Hi @Anil Sagar

i have just updated the last comment, you need to modify the javascript file as well

got the solution !!!

i have to use encodeURIComponent(uri) method before appending it to target url.

working javascript:

 var getPayload = context.getVariable("request.content");
var targetUrl = "http://google.com?visitorInfo="+encodeURIComponent(getPayload);
 context.setVariable("target.url",targetUrl);

Thanks for all the help @Anil Sagar. i really appreciate it 🙂

Check my below comment, That also should work. Any reason you are sending it as query param ? I don't think it's a great practice.

Dear @Vinay Soni ,

Try below code,

 var getPayload = context.getVariable("request.content");
 context.setVariable("request.queryparam.info", getPayload);

I don't think it's a good practice to send json as query param. Your url will be encoded since it contains special characters.

Cheers,

Anil Sagar