Javascript URI change - what happened to the query parameters?

Hi @Dino-at-Google, @williamssean, @Siddharth Barahalikar

Concern: Need to modify the Apigee proxy request uri before hitting the target endpoint.

Ex: Case 1:

APIGEE Endpoint: https://api.com/basepath/v1/al29/services/newrest/cat?cnumber=7700&a=23

Target Endpoint: https://al29.zforce.com/services/newrest/cat?cnumber=7700&a=23

Case 2: Change in code number (cll59) [this must be dynamic]

APIGEE Endpoint: https://api.com/basepath/v1/cll59/services/newrest/cat?cnumber=7700&a=23

Target Endpoint: https://cll59.zforce.com/services/newrest/cat?cnumber=7700&a=23

Case 3: Query parameters (if the APIGEE endpoint is having 3 query params then the target endpoint should also need to have 3 queryparams)

APIGEE Endpoint: https://api.com/basepath/v1/bll59/services/newrest/cat?cnumber=7700&a=23&d=qw

Target Endpoint: https://bll59.zforce.com/services/newrest/cat?cnumber=7700&a=23&d=qw

So, far using JavaScript I was able to modify the uri but not sure why the query params are not getting attached to target endpoint.

Regards,

Ashwith

1 3 416
3 REPLIES 3

You didn't show your code, so I don't know how you modified the URL.

I think the problem could possibly be that if you modify the URL and set target.url, you may overwrite (or inadvertently omit) any query params. To be clear, if you set target.url, then you need to explicitly append any query parameters you desire.

There is a nice URI parser module that you can use in a JS callout to help with performing such URI surgery. I recommend it.

For example, in this snip you can see the code sets target.url, but appends the query from the inbound:

var inboundUri = new URI(context.getVariable('proxy.url'));
var inboundPath = inboundUri.path();
var outboundUri = new URI(context.getVariable('target.url'));
var originalPath = outboundUri.segment();
var pathToAppend = inboundUri.segment();
outboundUri.segmentCoded(originalPath.concat(pathToAppend));
outboundUri.query(inboundUri.query());
context.setVariable('target.url', outboundUri.toString());

To use this module, you would have to download the URI.js file, store it in the resources/jsc directory, and then reference it from the JS policy like this:

<Javascript name='JS-SetTargetUrl'>
  <IncludeURL>jsc://URI.js</IncludeURL>
  <ResourceURL>jsc://setTargetUrl.js</ResourceURL>
</Javascript>

Thanks @Dino, @Dino-at-Google for the reply.

New concern,

Apigee endpoint is pointing to the target endpoint, when we fire a request to APG, backend is responding with the mentioned payload.

Backend-response:

{
  "name":"xyz",
"age": null,
"street": null

}

My requirement: This is how I want the response back to the consumer

{
  "name":"xyz",
"age": "",
"street": ""

}

Here we need to convert null to ""


Regards,

Ashwith

For a new question, please post a new question.

9474-ask-a-question.png