I've setup apigee proxy with an endpoint that target a google function and return "hello world".
i've also added a JS policy to apigee which sends me few properties about the request, i'm trying to get the proxy url but always getting the google function url which sits behind the apigee proxy.
what am i missing ?
Solved! Go to Solution.
Try putting your JS policy in the proxy request flow. Once the request gets to the target, the request URI gets changed to the target URI.
Also, note the scope for `request.url` it starts in the target response flow and for `request.uri` it starts in proxy request but changes. See: https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference#request
You cannot access Response variables in the Request flow.
In this situation, in the Request flow, I recommend you copy the original Request message object to preserve it and then you reference that object later. The message processor will not override any of the request properties in the new object. Now, you can access both unaltered Request and Response variables, but the policy reading those variables should always be in the Response flow.
First, use the AssignMessage policy to copy the message object into a new variable called 'request_msg'. This policy must be in the Request flow.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-CopyRequest">
<DisplayName>AM-CopyRequest</DisplayName>
<Properties/>
<Copy source="request">
<FormParams/>
<Headers/>
<Path>true</Path>
<Payload>true</Payload>
<QueryParams/>
<Verb>true</Verb>
<Version>true</Version>
</Copy>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="true" transport="http" type="request">request_msg</AssignTo>
</AssignMessage>
Then somewhere in the Response flow, you can read the variables like -
var proxy_request_uri = context.getVariable('request_msg.uri');
var target_request_uri = context.getVariable('request.uri');
var target_response_code = context.getVariable('response.status.code');
Try putting your JS policy in the proxy request flow. Once the request gets to the target, the request URI gets changed to the target URI.
Also, note the scope for `request.url` it starts in the target response flow and for `request.uri` it starts in proxy request but changes. See: https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference#request
Thank you so much, i was banging my head on this stupid issue 🙂
@kurtkanaskie how do i get the response status code if i attached the js policy to the proxy request section ?
You cannot access Response variables in the Request flow.
In this situation, in the Request flow, I recommend you copy the original Request message object to preserve it and then you reference that object later. The message processor will not override any of the request properties in the new object. Now, you can access both unaltered Request and Response variables, but the policy reading those variables should always be in the Response flow.
First, use the AssignMessage policy to copy the message object into a new variable called 'request_msg'. This policy must be in the Request flow.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-CopyRequest">
<DisplayName>AM-CopyRequest</DisplayName>
<Properties/>
<Copy source="request">
<FormParams/>
<Headers/>
<Path>true</Path>
<Payload>true</Payload>
<QueryParams/>
<Verb>true</Verb>
<Version>true</Version>
</Copy>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="true" transport="http" type="request">request_msg</AssignTo>
</AssignMessage>
Then somewhere in the Response flow, you can read the variables like -
var proxy_request_uri = context.getVariable('request_msg.uri');
var target_request_uri = context.getVariable('request.uri');
var target_response_code = context.getVariable('response.status.code');