Using javascript to dynamically update target backend user is giving null

when i use the code in target backend i get error user null....

if(context.flow=="PROXY_REQ_FLOW"){var username = context.getVariable("request.formparam.user");     context.setVariable("info.username", username);}if(context.flow=="TARGET_REQ_FLOW"){     context.setVariable("request.verb","GET");var name = context.getVariable("info.username");var url ="http://mocktarget.apigee.net/"     context.setVariable("target.url", url +"?user="+ name);}

Please help me..
0 6 214
6 REPLIES 6

sidd-harth
Participant V

Where are you adding this policy?

You need to add this JS policy to both Proxyendpoint and Targetendpoint request side.

You will get null if you only add this to Targetendpoint request, because the first IF condition never executes and the info.username is never Set.

I hope you are sending the user value as application/x-www-form-urlencoded

Still getting same error after uploading in proxy endpoint and target endpoint..

Share your Trace session here.

dynamicurl2.png dynamictargeturl1.png @ Siddharth Barahalikar

You should put this in the comments of the last thread instead of a new answer. this will confuse other members who seek help here.

Hi @HIMADRI BHATTACHARJEE

Here are the couple of things you need to fix,

You need to remove formparam as you are sending POST and converting that to GET. For that use Assign Message Policy in the proxy request flow

<Remove>
        <FormParams>
            <FormParam name="user"/>
        </FormParams>
    </Remove>

Also I have correct your JS also, instead of updating verb in Target Req Flow use it in Proxy Request flow.

if (context.flow == "PROXY_REQ_FLOW") {
    var username = context.getVariable("request.formparam.user");
    context.setVariable("info.username", username);
    context.setVariable("request.verb", "GET");
}
if (context.flow == "TARGET_REQ_FLOW") {
    var name = context.getVariable("info.username");
    var url = "http://mocktarget.apigee.net/";
    context.setVariable("target.url", url + "?user=" + name);
}

I have attached the working proxy sample.js-demo-rev1-2019-07-29.zip

Hope this helps.