Modifying proxy.pathsuffix to trim excess

Not applicable

Our backend service does not allow .xml or .json to be passed but we want to allow a front-end user to be able to specify their desired response format in this way.

If a user tries to hit GET {base-URL-stuff}/books.xml then that means they want all books returned in XML format. However, since our backend service cannot handle the .xml we need to only forward the request as /books (essentially just trim the .xml off of proxy.pathsuffix).

So far I have tried to add in a .JS policy that is executed on conditional flow if the proxy.pathsuffix = "/*.xml", this executes but I haven't been able to properly set the proxy.pathsuffix to the trimmed version (without the .xml). Whenever I do a trace the all is still sent to the backend as /books.xml which causes a 400 error. Below is the .js code, the trace for this policy always shows a "=" with a slash through it for proxy.pathsuffix and request.url so as to say that it cannot be set in this way.

Does anyone have a way to make this work?

var suffix = context.getVariable("proxy.pathsuffix");
var suffix2 = suffix.slice(-4);
var suffix3 = suffix.slice(0, -4);

try {
    if(suffix2 == ".xml"){
        var req_verb = context.getVariable('request.verb');
        var req_scheme = context.getVariable('client.scheme');
        var req_host = context.getVariable('request.header.host');
        var base_path = context.getVariable('proxy.basepath');
        context.setVariable('request.uri', suffix3);
        var req_request_uri = context.getVariable('request.uri');
        var req_url = req_scheme + "://" + req_host + base_path + req_request_uri;
        context.setVariable('request.url', req_url);
        print("Req url: " + req_url);
    }
}
catch(e){}
2 1 1,248
1 REPLY 1

Yes I understand what you're trying to do.

Here's what I suggest.

Instead of setting the request.url variable, set the target.url variable.

To do that, you need to run that JS policy in the Target flow.

So attach it to the Target Endpoint (in the request flow!), not to the Proxy Endpoint.

That ought to do it for you.

ps: I have used this URI.js module to good effect in JSC policies, to do exactly what you are doing. The JS code would look like this:

var uri = URI(context.getVariable('target.url'));
uri.path(context.getVariable('new.pathsuffix') ); // include your trimmed thing 
context.setVariable('target.url', uri.toString());

The policy needs to be configured like this:

<Javascript name='Javascript-DoMapping' timeLimit='200' >
  <DisplayName>Javascript-DoMapping</DisplayName>
  <IncludeURL>jsc://URI.js</IncludeURL>
  <ResourceURL>jsc://your-js-module-here.js</ResourceURL>
</Javascript>