Reset target.url when transforming path, but have response policies?

According to https://community.apigee.com/questions/4303/how-do-i-transform-the-path-between-the-proxy-and.html, the best practice for updating URI path is to set the target.url variable.

I've noticed that this causes downstream proxy response policies to be skipped, because the test to determine which policies to execute (when using the proxypath.suffix anyway) happens against the "current" value of target.url, not the "original" value.

Is there a best practice to restore the target.url transformed in the TargetServer flows so that we do see the correct response flow policies execute?

Solved Solved
1 2 658
1 ACCEPTED SOLUTION

Yes! This is a common frustration. Suppose in your proxy endpoint, you have a conditional flow with a condition like this:

<Flow name='flow-1'>
 <Condition>request.verb = "GET" AND proxy.pathsuffix = "/foo/bar"</Condition> 
 <Request>...</Request>
 <Response>...</Response>
</Flow>

And then suppose you have policy logic that runs, either in the proxy flow or in the targetendpoint flow, that modifies either request.verb or proxy.pathsuffix (or even target.url).

When the response message returns from the backend, control returns to the Apigee Proxy, and at that point Apigee Edge re-evaluates the condition! This may be counter-intuitive or surprising. But since the previously executed policy logic has modified the variables, the condition no longer evaluates to true, and any policies you have configured in the response flow will not run.

To work around that pitfall, you will need to save the state of the variables you change, and refer to those variables in the Condition. For example, in a preflow step, copy the request.verb and pathsuffix to orig_verb and orig_pathsuffix, and set your Condition to refer to THOSE.

<Flow name='flow-1'>
 <Condition>orig_verb = "GET" AND orig_pathsuffix = "/foo/bar"</Condition> 
 <Request>...</Request>
 <Response>...</Response>
</Flow>

To copy the variables, just use AssignMessage with two AssignVariable elements.

<AssignMessage name='AM-PreserveFlowVars'>
  <AssignVariable>
    <Name>orig_verb</Name>
    <Ref>request.verb</Ref>
  </AssignVariable>
  <AssignVariable>
    <Name>orig_pathsuffix</Name>
    <Ref>proxy.pathsuffix</Ref>
  </AssignVariable>
</AssignMessage>

..and attach that in the PreFlow / Request.

View solution in original post

2 REPLIES 2

Yes! This is a common frustration. Suppose in your proxy endpoint, you have a conditional flow with a condition like this:

<Flow name='flow-1'>
 <Condition>request.verb = "GET" AND proxy.pathsuffix = "/foo/bar"</Condition> 
 <Request>...</Request>
 <Response>...</Response>
</Flow>

And then suppose you have policy logic that runs, either in the proxy flow or in the targetendpoint flow, that modifies either request.verb or proxy.pathsuffix (or even target.url).

When the response message returns from the backend, control returns to the Apigee Proxy, and at that point Apigee Edge re-evaluates the condition! This may be counter-intuitive or surprising. But since the previously executed policy logic has modified the variables, the condition no longer evaluates to true, and any policies you have configured in the response flow will not run.

To work around that pitfall, you will need to save the state of the variables you change, and refer to those variables in the Condition. For example, in a preflow step, copy the request.verb and pathsuffix to orig_verb and orig_pathsuffix, and set your Condition to refer to THOSE.

<Flow name='flow-1'>
 <Condition>orig_verb = "GET" AND orig_pathsuffix = "/foo/bar"</Condition> 
 <Request>...</Request>
 <Response>...</Response>
</Flow>

To copy the variables, just use AssignMessage with two AssignVariable elements.

<AssignMessage name='AM-PreserveFlowVars'>
  <AssignVariable>
    <Name>orig_verb</Name>
    <Ref>request.verb</Ref>
  </AssignVariable>
  <AssignVariable>
    <Name>orig_pathsuffix</Name>
    <Ref>proxy.pathsuffix</Ref>
  </AssignVariable>
</AssignMessage>

..and attach that in the PreFlow / Request.

What I wound up doing was to reset the proxy.pathsuffix to the orig, so that I could still use the same flow for the inbound request and outbound response.