trouble rewriting a path when using a load balancer target

jerry
New Member

I was able to rewrite the url path using a javascript extention using the following code found at https://docs.apigee.com/api-platform/reference/policies/javascript-policy

if (context.flow=="TARGET_REQ_FLOW") {
    var url = context.getVariable("target.url");
    if (!url.endsWith("/")) {    
           url = url + "/";
    }
    context.setVariable("target.url", url + "greet/json");
}

When I start a load balancer as shown below however the javascript fails since target.url returns null. Should I be taking a different approach when using load balancers?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="default">
    <Description/>
    <FaultRules/>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>RewritePath</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <Flows/>
  <HTTPTargetConnection>
  	<LoadBalancer>
  		<Server name="growbackend"></Server>
  	</LoadBalancer>
  </HTTPTargetConnection>
</TargetEndpoint>
Solved Solved
0 6 496
2 ACCEPTED SOLUTIONS

Not applicable

When you are using load balancer variable, you need to set the path using path tag in the target endpoint.

below is the reference:

5th line in the code is for Path.

  <HTTPTargetConnection>
    <LoadBalancer>
      <Server name="target1" />
    </LoadBalancer>
    <Path>/test</Path>
  </HTTPTargetConnection>

View solution in original post

Not applicable

Hi Jerry,

you can do simply as below

proxy dns: https://apigee.com

base path: /api

proxy url: https://apigee.com/api

target path

<Path>/anura</Path>

Now anything after /api in the proxy url will automatically get appended to /anura as below you wanted

  • /api/people-search goes to /anura/people-search
  • /api/list/search goes to /anura/list/search
  • and so forth

View solution in original post

6 REPLIES 6

Not applicable

When you are using load balancer variable, you need to set the path using path tag in the target endpoint.

below is the reference:

5th line in the code is for Path.

  <HTTPTargetConnection>
    <LoadBalancer>
      <Server name="target1" />
    </LoadBalancer>
    <Path>/test</Path>
  </HTTPTargetConnection>

Can Apigee manipulate and extend incoming request paths?

Let's say this api proxy is for proxy base path /api/*

Can Apigee support the following type of rewrite?

  • /api/people-search goes to /anura/people-search
  • /api/list/search goes to /anura/list/search
  • and so forth

@Jerry Thomas,

The <Path> tag can be configured with variables. For example you can set it to the proxy path suffix as

<Path>/anura/{proxy.pathsuffix}</Path>

or you can extract the required path from the incoming url pathsuffix using extract variables or javascript policies and use it in the <Path> definition

<Path>/anura/{extractedpathsuffix}</Path>

Not applicable

Hi Jerry,

you can do simply as below

proxy dns: https://apigee.com

base path: /api

proxy url: https://apigee.com/api

target path

<Path>/anura</Path>

Now anything after /api in the proxy url will automatically get appended to /anura as below you wanted

  • /api/people-search goes to /anura/people-search
  • /api/list/search goes to /anura/list/search
  • and so forth

jerry
New Member

Will the <path> element work without a loadbalancer. I'm trying a simple proxy with no policies and the following TargetEndpoint and the <path> does not seem to be having an effect.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="default">
    <Description/>
    <FaultRules/>
    <PreFlow name="PreFlow">
        <Request/>
        <Response/>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <Flows/>
    <HTTPTargetConnection>
        <Properties/>
        <URL>http://34.74.32.6:8080</URL>
        <Path>/json/greeting</Path>
    </HTTPTargetConnection>
</TargetEndpoint>

Not applicable

No, the path will not work without load balancer variable.

If you need, then you can use target url set dynamically.