How to dynamically set target URL?

Not applicable

Hi, My requirement is to create a new general purpose API proxy that will dynamically set the target URL and therefore capable of forwarding payloads to different URLs of the same or different origin systems.

  • Authentication: OAuth
  • Target endpoint: dynamic, grammatically obtained from "s_url" from within the payload.
Solved Solved
1 7 12.9K
1 ACCEPTED SOLUTION

yes, the correct use of the AssignMessage policy in this case is:

<AssignMessage name="Assign-Message-1">
    <AssignVariable>
        <Name>target.url</Name>
        <!-- <Value>{apigee.eventName}</Value>  NO-->
        <Ref>apigee.eventName</Ref>  <!-- YES -->
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

The Value element is used to set a specific value. The text of the Value element is not interpreted as a Message Template. (Wrapping variable names in curlies inside the Value element... does nothing! Though there is an outstanding feature request to allow something like this. (** See below for an update on this))

Be aware: Writing the target.url is effective only when executed in the request flow of the target endpoint.


EDIT 2021 July - There has been, for some time now, an additional option within the AssignVariable element, to allow message templates. It is the Template element. it's pretty simple to use. It looks like this:

<AssignMessage name="Assign-Message-1">
    <AssignVariable>
        <Name>target.url</Name>
        <!-- <Value>{apigee.eventName}</Value> does nothing -->
        <Template>{foo} fixed text {bar}</Template> 
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

Readers may want to check the documentation for precedence and etc.

View solution in original post

7 REPLIES 7

@neha , Welcome to Apigee Community. You can able to achieve same using route rules. Did you get a chance to play with same ? Additionally, You can also use javascript to change target url dynamically. Find more regarding same here.

You can use Extract Variable policy to extract the value of the 's_url' tag from within the request payload.

Once this is done, you can use the Assign Message policy or a Javascript to set this value to the 'target.url' variable. Make sure that this is done in the Target Endpoint as this variable's scope is restricted.

Hi @Hari,

Thanks for your reply.

I tried the same but my assign-message-policy not reading json variable.

ExtractVariable policy is attached to PreFlow and Assign-Message is attached to target endpoint preflow.

My Payload is:

{

s_url: "http:abc/xyz/"

}

PFB my policies:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1">
    <DisplayName>Extract Variables-1</DisplayName>
    <Properties/>
    <URIPath name="name"/>
    <QueryParam name="name"/>
    <Header name="name"/>
    <FormParam name="name"/>
    <Variable name="name"/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
       <Variable name="eventName">
            <JSONPath>$.s_url</JSONPath>
       </Variable>
    </JSONPayload>
    <Source clearPayload="false">request</Source>
    <VariablePrefix>apigee</VariablePrefix>
</ExtractVariables><br>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>
    <Copy source="request">
        <Headers/>
        <QueryParams/>
        <FormParams/>
        <Payload/>
        <Verb/>
        <StatusCode/>
        <ReasonPhrase/>
        <Path/>
    </Copy>
    <Remove>
        <Headers>
            <Header name="h1"/>
        </Headers>
        <QueryParams>
            <QueryParam name="q1"/>
        </QueryParams>
        <FormParams>
            <FormParam name="f1"/>
        </FormParams>
        <Payload/>
    </Remove>
    <Add>
        <Headers/>
        <QueryParams/>
        <FormParams/>
    </Add>
    <Set>
        <Headers/>
        <QueryParams/>
        <FormParams/>
        <!-- <Verb>GET</Verb> -->
        <Path/>
    </Set>
    <AssignVariable>
        <Name>target.url</Name>
        <Value>{apigee.eventName}</Value>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage><br>

Hi @neha

Sorry for the long delay! In all probability, you have already solved this issue. If you have, let me know what the solution was.

yes, the correct use of the AssignMessage policy in this case is:

<AssignMessage name="Assign-Message-1">
    <AssignVariable>
        <Name>target.url</Name>
        <!-- <Value>{apigee.eventName}</Value>  NO-->
        <Ref>apigee.eventName</Ref>  <!-- YES -->
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

The Value element is used to set a specific value. The text of the Value element is not interpreted as a Message Template. (Wrapping variable names in curlies inside the Value element... does nothing! Though there is an outstanding feature request to allow something like this. (** See below for an update on this))

Be aware: Writing the target.url is effective only when executed in the request flow of the target endpoint.


EDIT 2021 July - There has been, for some time now, an additional option within the AssignVariable element, to allow message templates. It is the Template element. it's pretty simple to use. It looks like this:

<AssignMessage name="Assign-Message-1">
    <AssignVariable>
        <Name>target.url</Name>
        <!-- <Value>{apigee.eventName}</Value> does nothing -->
        <Template>{foo} fixed text {bar}</Template> 
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

Readers may want to check the documentation for precedence and etc.

We can not set the request.header.origin values here , can we set this ? not getting same header origin in the response

Ask a new question please.