AssignMessage Set Path won't work

VK
Bronze 1
Bronze 1

Here my AssignMessage:

 

 

<AssignMessage continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>
    <Set>
        <Path>/get</Path>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

 

 

and it won't set path.
The main idea here: I have target endpoint: https://httpbin.org
and I need at runtime add path to it /get or /post,
here in my example I have only /get just for simplicity.

Could someone please help figure out how to set path in AssignMessage?

Here you can find all apiproxy xml files (apiproxy.1.zip).

0 9 275
9 REPLIES 9

Attach this to the Target Request Preflow. 

<AssignMessage name="AM-Override-Path">
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>

  <!--
      By setting target.url to a HTTP URL, you can override
      what is specified in the TargetEndpoint configuration.
      In this case, I append a specific path.  But I could
      overwrite it with a completely different URL.

      You can do the similar thing in a JS policy with
      a call to context.setVariable().
  -->
  <AssignVariable>
    <Name>target.url</Name>
    <Template>{target.url}/over-ridden-path</Template>
  </AssignVariable>
</AssignMessage>

It works when in TargetEndpoint I have:

 

    <HTTPTargetConnection>
        <URL>https://httpbin.org</URL>
    </HTTPTargetConnection>

 

but it won't work when:

 

    <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="dbgServer"/>
        </LoadBalancer>
    </HTTPTargetConnection>

 

Here apiproxy xml files (apiproxy.2b.zip).

Is there a way to rewrite path when using target server?

Yes, AFAIK you can use the Path element when using the LoadBalancer.

    <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="dbgServer"/>
        </LoadBalancer>
       <Path>{path-variable-here}</Path>
    </HTTPTargetConnection>

This Path element won't work with the URL element.  It works only with the LoadBalancer element. 

It's not ideal that you need to use two different ways to dynamically set the path, depending on whether you use a LoadBalancer or not. But that's where we are.

 

It's semi working and looks like it's doing merge:

when request to apigee: https://example.$pid.apigee.internal/dbg/
then request to target: https://httpbin.org/get?foo=bar

when request to apigee: https://example.$pid.apigee.internal/dbg/x/y/some/path?q=9
then request to target: https://httpbin.org/get/x/y/some/path?foo=bar&q=9

Is there a way to rewrite whole path completely?

Here apiproxy xml files (apiproxy.2c.zip) for this example.

I don't know what you mean by "completely". 

The default behavior of the API Proxy in Apigee is to propagate the pathsuffix into the target request. That is why you get /x/y/some/path appended to your target path.  That /x/y/some/path is the pathsuffix in the request sent into Apigee.

If you don't want this behavior, you can set the variable target.copy.pathsuffix to false.  Then, the /x/y/some/path won't get appended to your target path. 

 

@dchiesa1 I've found missing part:

<AssignMessage continueOnError="false" enabled="true" name="Assign-Message-2">
    <DisplayName>Assign Message-2</DisplayName>
    <AssignVariable>
        <Name>target.copy.pathsuffix</Name>
        <Value>false</Value>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

Now it works! Thank you!

Follow below steps

create 2 conditional flows of /get and /post

in /get conditional flow add an assign message with below value

<AssignMessage continueOnError="false" enabled="true" name="Assign-Message-get">
<DisplayName>Assign Message-get</DisplayName>
<Properties/>
<AssignVariable>
<Name>targetpathsuffix</Name>
<Value>/get</Value>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

 

 

in /post conditional flow add an assign message with below value

<AssignMessage continueOnError="false" enabled="true" name="Assign-Message-post">
<DisplayName>Assign Message-post</DisplayName>
<Properties/>
<AssignVariable>
<Name>targetpathsuffix</Name>
<Value>/post</Value>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

 

 

in target mention base path as below

<HTTPTargetConnection>
<URL>https://httpbin.org</URL>
<Path>{targetpathsuffix}</Path>
</HTTPTargetConnection>

 

 

let me know how it goes

 

Unfortunately, it won't work.

Here apiproxy xml files (apiproxy.3.zip).

And I need not use

 

<URL>https://httpbin.org</URL>

 

but use:

 

<LoadBalancer>
    <Server name="dbgServer"/>
</LoadBalancer>

 

<LoadBalancer>
    <Server name="dbgServer"/>
    <Path>{targetpathsuffix}</Path>
</LoadBalancer>

the  use like this