JS rewriting target endpoint query.

sidd-harth
Participant V

PFA of my proxy.

Targetendpoint url is api.usergrid.com

I created a js to re-write the target base on a uri param, uri param is extracted thrpough extract variable policy.

context.setVariable("target.copy.pathsuffix", false);
var name =context.getVariable("name");
var targetSuffix="/siddharth1/sandbox/volvos";
var targetpathSuffix="";
 if(name !== null ){
  targetpathSuffix=targetSuffix+"/"+name;
    context.setVariable("targetpathSuffix", targetpathSuffix);
  } 

In trace i see that 'targetpathSuffix' is set,

targetpathSuffix - /siddharth1/sandbox/volvos/Harris

But it is not appended to the Request sent to the target server URL

3047-11.pngjs-rev1-2016-06-29.zip

You can hit, http://siddharth1-test.apigee.net/abc/Harris

@Anil Sagar

Solved Solved
2 10 871
1 ACCEPTED SOLUTION

Not applicable

Hi Siddharth,

Can you Please try the below?

//context.setVariable("target.copy.pathsuffix", false);
var name =context.getVariable("name");
//var targetSuffix="/siddharth1/sandbox/volvos";
if(name !== null ){
  var targeturl = context.getVariable("target.url");
  context.setVariable("target.url", targeturl+"/siddharth1/sandbox/volvos/"+name);
}

Regards

Binaya

View solution in original post

10 REPLIES 10

Not applicable

Do you have this policy in Proxy flow or Target flow? I believe it needs to be in the Target flow to have access to modify target variables.

Hi @clatimer1, it is in target preflow.

Actually looking closer, it doesn't look like targetpathSuffix is a predefined variable. Can you try setting target.url instead?

yes, as clatimer said, you must set target.url. And in a target request flow. (preflow is ok, but it must be request preflow).

Not applicable

Hi Siddharth,

Can you Please try the below?

//context.setVariable("target.copy.pathsuffix", false);
var name =context.getVariable("name");
//var targetSuffix="/siddharth1/sandbox/volvos";
if(name !== null ){
  var targeturl = context.getVariable("target.url");
  context.setVariable("target.url", targeturl+"/siddharth1/sandbox/volvos/"+name);
}

Regards

Binaya

Nice code. For cases where you're not sure about the slashes, I have a helper function to join url path elements.

function joinUrlElements() {
  var re1 = new RegExp('^\\/|\\/$', 'g'),
      elts = Array.prototype.slice.call(arguments);
  return elts.map(function(element){
    if ( ! element) {return '';}
    return element.replace(re1,""); }).join('/');
}

And the way you would use it, is like this:

  context.setVariable("target.url",
                      joinUrlElements(targeturl, "/siddharth1/sandbox/volvos", name));

That function takes care to insert slashes as necessary between the elements, and to eliminate (or avoid) double-slashes.

Thanks @Binaya Kumar lenka for a clean approach. I got to know my mistake by @clatimer1 comments, about using target.url.

@Dino, I will be using your function for an other user case, thank you for the code.

sidd-harth
Participant V

Hi @Dino @Anil Sagar @Binaya Kumar lenka @clatimer1 @AMAR DEVEGOWDA, what if I am using Target Servers?

How can I append from JS to target server url?

you must set a variable with a name like servicecallout.XXX.target.url, where XXX is replaced by the policy name of the ServiceCallout policy.

context.setVariable('servicecallout.SC-1.target.url',
                    joinUrlElts(base, elt1, elt2));

Or, you can set two variables; the one with the well-known name for the base, and another one, with a name you choose, for the path and search. Then in the policy you would do this:

<ServiceCallout name='SC-SendAnotherRequest'>
  <DisplayName>SC-SendAnotherRequest</DisplayName>
  <Request variable='extraRequest'>
    <Set>
      <Verb>GET</Verb>
      <!-- this var gets set by Javascript callout -->
      <Path>{sc_urlPath}</Path>
    </Set>
  </Request>
  <Response>extraResponse</Response>
  <HTTPTargetConnection>
    <Properties>
      <Property name='success.codes'>2xx, 4xx, 5xx</Property>
    </Properties>
      <!-- a var servicecallout.XXX.target.url will be used here,
           implicitly.
      -->
    <URL>http://ignore-this/it-gets-overwritten</URL>
  </HTTPTargetConnection>
</ServiceCallout>

@Dino Why are we using service callout. Sorry the whole thing went over my head.

I have a target server which I have mentioned in Target end point HTTPTargetConnection.

Now I want to append URL through Javascript. I know that we cannot access target.host in target endpoint request.

<HTTPTargetConnection>
        <Properties/>
        <SSLInfo>
            <Enabled>true</Enabled>
        </SSLInfo>
        <LoadBalancer>
            <Server name="Book-List"/>
        </LoadBalancer>
        <Path>/v1/books</Path>
        
    </HTTPTargetConnection>

I really did not understand the service callout thing you explained.