Reputation: 7809
I want to transform a URL before it is redirected to the target, removing a string. I have the following flow in Apigee:
USER -> APIGEE -> APPLICATION -> APIGEE -> USER
The user requests and then its URL should be rewritten removing bar
from the URL.
BEFORE apigee.url.com/foo/bar/pBxATaojIn8tk5dvQdNJ
AFTER target.url.com/foo/pBxATaojIn8tk5dvQdNJ
I use Proxy Endpoints and Target Endpoints and try to rewrite using a PreFlow hook with Javascript in the Target Endpoint, without success rewriting the proxy.pathsuffix
. How can I solve this?
Upvotes: 1
Views: 4072
Reputation: 1
I use this script to do this
// Replace string in incoming proxy URL path
var proxyurl = context.getVariable("proxy.url").trim();
var targeturl = context.getVariable("target.url").trim();
var pathsuffix = context.getVariable("proxy.pathsuffix").trim();
var qpa = [];
if(proxyurl.split("?").length > 1) {
queryparam = proxyurl.split("?")[1]
qpa = qpa.concat(queryparam.split ("&"));
}
if(targeturl.split("?").length > 1) {
queryparam = targeturl.split("?")[1]
qpa = qpa.concat(queryparam.split ("&"));
}
context.setVariable("target.copy.pathsuffix", false);
if(targeturl.split("?").length > 0) queryparam = targeturl.split("?")[1]
var url = targeturl.split("?")[0] + pathsuffix.split("/toberemoved")[1];
if (url.slice(-1) !== "/") url +="/";
if (qpa.length > 0){
url+= '?' + qpa.join("&");
}
context.setVariable("target.url", url);
Upvotes: 0
Reputation: 1828
I solved this similar to how you did it. See my answer on another SO question that answers this.
Upvotes: 0
Reputation: 223
From my understanding proxy.pathsuffix
is a read only variable so you can't override it.
Assign your target endpoint to target.url
instead. (I usually use in Assign Message Policy of the request)
Example:
<AssignVariable>
<Name>target.url</Name>
<Ref>yourTargetUrlValue</Ref>
</AssignVariable>
In my Assign Message Policy of the request, I have added the block of code above for defined my target url.
You can assign directly like <Ref>http://test.com</Ref>
or assign via variable like <Ref>{targetUrlVal}</Ref>
If you got the url from somewhere, don't forget to assign value to your variable before use it by using context.setVariable("targetUrlVal", "http://test-01.com");
Hopefully my answer will help you.
Upvotes: 0
Reputation: 7809
I now use the following solution:
// Disable copy path
context.setVariable("target.copy.pathsuffix", false);
// Replace string in incoming proxy URL path
var proxyPathSuffix = context.getVariable("proxy.pathsuffix");
var fooBarAfter = proxyPathSuffix.replace('/fooToReplace', '');
// Fetch target outgoing url path
var targetBasePath = context.getVariable("target.basepath");
var urlPath = targetBasePath.concat(fooBarAfter);
// Replace outgoing url
var targetUrl = context.getVariable("target.url");
targetUrl = targetUrl.replace(targetBasePath, urlPath);
context.setVariable("target.url", targetUrl);
I came up with it looking at the available variables here. As this is JS, if someone comes up with a better solution I would be happy!
Upvotes: 1