How do i get the proxy url of apigee proxy within js policy

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.


tried all the following, but alway getting the google function url instead:

//TODO: domain is not sent , it always bring the google function url for some reason
const req_url = context.getVariable('request.uri');
const requestUrl = context.getVariable('request.url')
const req_verb = context.getVariable('request.verb');
const proxyurl = context.getVariable('proxy.url');
const req_host = context.getVariable('request.header.host');


what am i missing ?

Solved Solved
0 4 684
2 ACCEPTED SOLUTIONS

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

View solution in original post

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');

 

View solution in original post

4 REPLIES 4

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');