Various uses of Assign Message Policy

6 3 10.6K

AssignMessage is an extremely diverse policy using which you can do a wide variety of things. There is good amount of descriptions on the AssignMessage documentation page but I am adding a few more here:

Set Up URL Redirections

<Set>
        <Headers>
            <Header name="Location">{config.protocol}://{config.organization}-{config.environment}.{config.domain}/web?access_token={access_token}</Header>
        </Headers>
        <StatusCode>302</StatusCode>
        <ReasonPhrase>Redirect</ReasonPhrase>
    </Set>

Setup multiple variables

This is needed if you want to setup a number of global variables at once to be referenced by other policies later in the pipeline.

    <AssignVariable>
        <Name>appSecret</Name>
        <Value>abcdedl1234</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>config.environment</Name>
        <Value>test</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>config.protocol</Name>
        <Value>https</Value>
    </AssignVariable>

CORS enable your APIs

Below is for CORS-Preflight

<Set>
     <Headers>
        <Header name="Access-Control-Allow-Origin">*</Header>
        <Header name="Access-Control-Allow-Methods">POST, GET, OPTIONS</Header>
        <Header name="Access-Control-Allow-Headers">Accept, Content-Type, Authorization, x-requested-with</Header>
        <Header name="Access-Control-Allow-Credentials">true</Header>
     </Headers>
</Set>

Setup a target url different than what is setup at target.xml.

Or also known as dynamic target. If based on a condition you need to manipulate the URL. You can do the same using javascript as well and javascript often adds more flexibility , but if the requirements are simple AssignMessage can do the trick as well.

<AssignVariable>
        <Name>target.url</Name>
        <Template>https://api.usergrid.com/orgName/sandbox/products/{myretail.productid}/has/reviews</Template>
    </AssignVariable>

Setting custom error messages

<Set>
        <Payload contentType="application/json">{
  "error": {
    "message" : "Not Found",
    "detail" : "The requested resource was not found."
} }
</Payload> <StatusCode>404</StatusCode> <ReasonPhrase>Not Found</ReasonPhrase> </Set>

If anyone has any other interesting use of the AssignMessage policy please add them in the comments.

Comments
karthickvankaya
New Member

Could you help me how to pass a custom header to back end using this policy if it permits?

My use case is I have created a KVM containing my App key. I want my proxy to inject this app key as a header to target request message.

Appreciate your help in advance!

anilsr
Staff

@Karthick Vankayala ,

Welcome to Apigee Community.

You need to use KVM policy to retrieve the value from KVM & use Assign Message policy to set a header which will be sent to target in request flow. Have you tried something ? do you see any errors ? It should be straight forward. Keep us posted.

rohanjangid
Bronze 5
Bronze 5

sir how can we display in body please have a look for proper documentation https://community.apigee.com/questions/90058/how-to-display-developer-details-when-ever-we-hit.html

Version history
Last update:
‎10-02-2015 04:44 PM
Updated by: