Javascript: context.proxyRequest.url ===context.targetRequest.url ????

Not applicable

I need to make a script that parses the response context and replaces any references to the original target domain to the proxy domain.

To start testing this i added a Javascript extension with the following code:

if (context.flow=="PROXY_RESP_FLOW") {
  context.proxyResponse.content="{\""+context.proxyRequest.url+"\":\""+context.targetProxy.url+"\"}"; 
}

Which i expected to output my proxy domain and the target domain, but instead:

{"http://mytargetdomain.com/endpoint/":"http://mytargetdomain.com/endpoint/"}

Is this the expected result??? if so, how can i get the actual proxy domain (http://org-env.apigeee.net/proxypath)

1 8 1,537
8 REPLIES 8

Dear @luisvillanueva1 ,

Welcome to Apigee Community . You mean proxy.url === target.url ? As far as i know, There is no flow varaible called "context.proxyRequest.url"

Well i am refering to the variables described in the Javascript Object Model http://docs.apigee.com/api-services/reference/javascript-object-model#contextobjectreference

There you can see where i got those variables.

The context object has global scope. It is available everywhere within the API proxy flow. It has four child objects: proxyRequest, proxyResponse,targetRequest, targetResponse.

@luisvillanueva1 , Ahhh got it. My bad, Thank you for more details.

@luisvillanueva1 ,

It's not equal, I see it as a bug when it comes to runtime. Doc says,

Four child objects:proxyRequest, proxyResponse, targetRequest, targetResponse. These child objects are scoped to the ambient request and response, either the proxy request and response or the target request and response. For example, if theJavaScript policy executes in the proxy endpoint part of the flow, then the context.proxyRequest andcontext.proxyResponse objects are in scope. If the JavaScript runs in a target flow, then thecontext.targetRequest and context.targetResponse objects are in scope.

That means, When you attach the policy to one of the proxy / target request endpoints you can use context.proxyRequest.url , context.targetRequest.url respectively. That means, you cannot use context.targetRequest in proxy request & context.proxyRequest.url in target request. Ideally, It should print null if you use, but it prints same as of today. I see this as a bug in the system. I will let engineering team know about this.

What happens if you use, context.proxyRequest.url in proxy & context.targetRequest.url in target endpoints separate javascript policies ?

Let's say,

Proxy URL is : http://anildevportal-test.apigee.net/mockjson

Target URL is : http://mocktarget.apigee.net/json

Then ,

context.proxyRequest.url outputs : /mockjson (Ideally , I expect , http://anildevportal-test.apigee.net/mockjson , which i see as an issue )

context.targetRequest.url outputs : http://mocktarget.apigee.net/json

See sample proxy below,

mockjson-rev2-2016-06-17.zip

Coming to your query,

You would need to construct proxy url in proxy request flow & assign to a custom flow variable, get target url from context.targetRequest.url in target request flow & assign to a custom flow variable. Use custom flow variables in target / proxy response flows to construct output content.

2943-screen-shot-2016-06-17-at-105631-pm.png

See attached proxy below which demonstrates same,

mockjson-rev3-2016-06-17.zip

Keep us posted if you have any queries. Hope it's helpful.

I agree the values should return null if they are out of scope. In that way I could detect what flow I am in when the policy executes in a shared flow, because context.flow is always SHARED_FLOW then.

when the policy executes in a shared flow, because context.flow is always SHARED_FLOW then.

There is a bug asking to change this behavior: b/127826421

Apigee - Jira - MGMT-3378