How can we build dynamic URL using JavaScript coding..

I have a case where the customer is having 5 fields (A, B, C, D, E). Customer can send any of the parameter or combination its like permutation and combination. Based on the request I need to form dynamic URL, but here the case is like if customer is sending A,B value then the URL should form only with AB value. 

As of now I am using ExtractVariable policy and extracting the parameters which is sending in query and in JavaScript we implemented if-else-if condition. This is not satisfied by the customer.

Please let me know how I can solve this...

Thanks,

0 3 2,123
3 REPLIES 3

As of now I am using ExtractVariable policy and extracting the parameters which is sending in query and in JavaScript we implemented if-else-if condition. This is not satisfied by the customer.

Hi Rajesh

Can you give some specifics? Show us a set of illustrative example inputs and outputs? Eg, input URL looks like /a/B, output URL should be: ??? And give a SET OF THEM. Not just one. Something that would illustrate what you aim to accomplish. And then give me an example of what you're doing in JS? and also explain why your customer is not satisfied with this solution?

In general you can perform string parsing in the JS policy, which would allow you to assemble target URL strings dynamically, based on whatever criteria you like: request input, environment state, a routing table, time of day, or whatever.

Hi Dchiesa,

Customer sending in query parameter like this below URL, Its just an example.

/v1/order?A=1235&B=xyz&C=RAN&D=6242&E=1234, but this is not fixed URL, they can send AB or BC or CD or ED or some combination. Based on this query parameters we need to form a customized URL like this /v1/order?A eq '1235' and B eq  'xyz' and C eq 'RAN' and D eq '6242' and E=1234 backend is expecting.

As of now I have created the URL based of if-else-if condition.

Please let me know how I can solve this...

Thanks,

Your desired target URL doesn't make any sense. That's not how query parameters work.  Are you sure there is a target system that needs that kind of input?  Your technical use-case seems quite contrived.

In any case you could do that relatively easy with a pair of .replaceAll() calls on the request.querystring variable. 

var targetQuery = 
  context
    .getVariable('request.querystring')
    .replaceAll('&', ' and ')
    .replaceAll(/=([^ ]+)/g, " eq '$1'");

Then you need to apply that to the target.url variable. Which you can do via context.setVariable().