1

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?

lony
  • 6,733
  • 11
  • 60
  • 92

3 Answers3

1

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!

lony
  • 6,733
  • 11
  • 60
  • 92
0

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.

Pim H
  • 223
  • 2
  • 8
  • Do you have an example? My problem is that my target.url is different and I therefore have to get it from somewhere. – lony Aug 19 '19 at 06:51
  • Okay, I have added something in my answer. Please check it. – Pim H Aug 22 '19 at 04:12
  • Thanks. As I understand it I would need a JS file to prepare the variable and then paste it using the assignVar policy. Right? – lony Aug 22 '19 at 06:58
  • Hm, then I would prefer my solution as it is solving this in one place avoiding an additional policy. Even policies are faster. – lony Aug 23 '19 at 08:52
  • 1
    Okay, but the JS Policy doesn't take too much time, fron what i've done it is take time less than 1 milli seconds. But yeah, choose the solution which is suit your scenario :) – Pim H Aug 26 '19 at 02:02
0

I solved this similar to how you did it. See my answer on another SO question that answers this.

Dan H
  • 1,828
  • 2
  • 21
  • 38
  • Please add your solution as your reference to your question with does not include code and links to Github – lony Aug 22 '19 at 14:35
  • Instead of linking to a GitHub project you want em to paste the code in the answer? – Dan H Aug 23 '19 at 13:47
  • A minimal viable solution in SO is better than linking to an external source, here you referenced the link to another answer which then references to Github. This for me seems not a good practice. – lony Aug 23 '19 at 14:01