Javascript Policy Not Working for Target.url Update Runtime

I am trying to update the target.url in my edge micro proxy with context setter but it's not working.

I am hitting this url -> 

 

 

 

https://xyz-eval-test.apigee.net/v1/edgemicro_proxy_ms/rel/1/pro/2/ws/3/stage/4/techstack/t4/techstackIp/52.170.150.5:8082?techstack=techstack-java

 

 

 

 

 

 

 

 

here is my project structure:

EV-URI-EXTRACT

 

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-URI-EXTRACT">
    <DisplayName>EV-URI-EXTRACT</DisplayName>
    <Properties/>
    <URIPath>
        <Pattern ignoreCase="true">/rel/{rId}/pro/{pId}/ws/{wId}/stage/{sId}/techstack/{techstackId}/techstackIp/{techstackIp}</Pattern>
    </URIPath>
    <QueryParam name="techstack">
        <Pattern>techstack-{techstackName}</Pattern>
    </QueryParam>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <Source clearPayload="false">request</Source>
</ExtractVariables>

 

 

 

 

 

 

 target-rewrite

 

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="target-rewrite">
    <DisplayName>target-rewrite</DisplayName>
    <Properties/>
    <ResourceURL>jsc://js-example.js</ResourceURL>
</Javascript>

 

 

 

 

 

 

proxy-endpoint

 

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <Description/>
    <FaultRules/>
    <Flows/>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>EV-URI-EXTRACT</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <HTTPProxyConnection>
        <BasePath>/v1/edgemicro_proxy_ms</BasePath>
        <Properties/>
        <VirtualHost>default</VirtualHost>
        <VirtualHost>secure</VirtualHost>
    </HTTPProxyConnection>
    <RouteRule name="default">
        <TargetEndpoint>default</TargetEndpoint>
    </RouteRule>
</ProxyEndpoint>

 

 

 

 

 

 

target-endpoint

 

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="default">
    <Description/>
    <FaultRules/>
    <Flows/>
    <PostFlow name="PostFlow">
        <Request>
        </Request>
        <Response>
        </Response>
    </PostFlow>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>target-rewrite</Name>
            </Step>
        </Request>
        <Response>
        </Response>
    </PreFlow>
    <HTTPTargetConnection>
        <Properties/>
        <URL>https://mocktarget.apigee.net/</URL>
    </HTTPTargetConnection>
</TargetEndpoint>

 

 

 

 

 

 

js-exampls.js

 

 

 

 

 

 

if (context.flow=="TARGET_REQ_FLOW") {
     print(context.getVariable("techstackIp")) // PRINTS correct value
     context.setVariable("target.url", context.getVariable("techstackIp"));
     print(context.getVariable("target.url"))// PRINTS In-correct value, prints default target uri
     context.setVariable("target.copy.pathsuffix", false);
}

 

 

 

 

 Any Help? Can someone please help me find out the mistakes I am doing here ?

5 REPLIES 5

You must set the target.url variable to a URL, not an IP address.

URL example:  https://foo.bar.whatever.com/path/goes/here

IP address example: 52.170.70.5:3443

Hi @dchiesa1 

Even I tried that- google.com and https://google.com but no luck.

Thanks for your reply!

But eventually my main Question/GOAL is below:

I want to call apigee edge proxy from edge microgateway and want to change the target.url dynamically. so let's say I will make a call like below examples:
call 1 -> v1/edgemicro_proxy_ms/ip/100.12.12.12

call 2 -> v1/edgemicro_proxy_ms/ip/101.13.13.13

call 2 -> v1/edgemicro_proxy_ms/ip/102.14.14.14

So How can I achieve this ? If I need to use some plugin in microgateway then please redirect me there? or please let me know how can I achieve this with some js policy or any other mechanism.

I did not realize you were using edge. microgateway.

Policies that you apply in Apigee Edge proxies do not affect the behavior of Edge microgateway. To affect the URL in microgateway you need to write a microgateway plugin.  

@dchiesa1 

I saw one post reply from you :

return {
      onrequest: function (req, res, next) {
        if (req.targetPath.includes(pathPrefix)) {
          req.targetHostname = newTargethost;
          req.targetPath = newUrl;
          req.targetPort = 80;
        }
        next();
      }
};

My requirement is exactly same as this post. where you replied.


1. Can you please share me full plugin code if you can ?

2. As per my origin question I do have one edgemicro proxy which needs updating target.url dynamically and in that I was not successful in achieving this even if my js target-rewrite was in target endpoint flow.... but then I realised that it would be better to use plugin in edgemicro gateway which will update target.url dynamically inside my edgemicro proxy which is deployed in edge ui but don't know how I ll do that ?

I think you and I are having the same dialogue on two different threads. 

I have previously replied that I do not have a full plugin code for you. 

I also understand that you don't have confidence in your ability to write JavaScript. 

In that case I suggest that you get some outside help to write your plugin.