Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

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,329
1 REPLY 1