Was this helpful?

The AssignMessage policy is used to generate or modify messages during an API proxy Flow. The messages that are generated or modified can be used, for example, by a subsequent ServiceCallout policy as a request to a remote service or API.

AssignMessage is so named because the policy requires a message to be 'assigned' as a variable. Therefore to use AssignMessage you select any variable and assign a message to it.

AssignMessage is often used to generate the request message sent by a ServiceCallout policy to a remote HTTP service.

In the following example, the request message generated by the AssignMessage policy is assigned to a variable called partner.request. The message generated is available to later policies or code executing in the flow, which can access the message through the variable.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage enabled="true" continueOnError="false" async="false" name="partnerRequest">
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="request">partner.request</AssignTo>
</AssignMessage>

The following AssignMessage policy creates a new request message, populated with the user-agent HTTP header from the source message, along with one query parameters derived from the source. Since the message generated by this policy is a request, the HTTP verb is also set to GET.

<AssignMessage name="GenerateRequest">
  <AssignTo createNew="true" type="request">Request</AssignTo>
  <Copy>
	<Headers>
	  <Header name="user-agent">{request.header.user-agent}</QueryParam>
	</Headers>
  </Copy>
  <Set>
    <QueryParams>
      <QueryParam name="address">{request.queryparam.address}</QueryParam>
    </QueryParams>
    <Verb>GET</Verb>
  </Set>
</AssignMessage>

The following AssignMessage policy does not create a new message. Instead, it modifies an existing response message by adding an HTTP header. The HTTP header added to the response message by this policy is derived from a variable populated by the LookupCache policy. Thus, the response message modified by this AssignMessage policy contains an HTTP header that indicates whether the results have been pulled from the Cache or not.

<AssignMessage enabled="true" continueOnError="false" async="false" name="api-token-cache-hit-header">
    <AssignTo createNew="false" transport="http" type="response">response</AssignTo>
    <Set>
        <Headers>
            <Header name="API-Token-Cache-Hit">{lookupcache.api-token-lookup-cache.cachehit}</Header>
        </Headers>
    </Set>
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</AssignMessage>

Working samples of the AssignMessage policy are available in the API Platform samples.

Configuring an AssignMessage policy

Configure the AssignMessage policy using the following elements.

Field Name Description
AssignTo (Optional)
Specifies the variable to which the message will be assigned.
  • If the <AssignTo> element is missing, it is treated as <AssignTo type="{decided based on flow to which the AssignMessage policy is attached: request or response}"/>.
  createNew
If createNew is set to true, a new variable of the type specified in <type> is created.
If createNew is set to false, <AssignTo> responds in one of two ways:
  • If <AssignTo> resolves to a message, it continues to the next flow.
  • If <AssignTo> cannot be resolved, or resolves to a non-message type, it fails to respond.
If createNew is not specified, <AssignTo> responds in one of two ways:
  • If <AssignTo> resolves to a message, it continues to the next flow.
  • If <AssignTo> cannot be resolved, or resolves to a non-message type, a new variable of type specified in <type> is created.
type
Specifies the type of the variable.
Valid values: request/response
Default value: request
IgnoreUnresolvedVariables (Optional)
If IgnoreUnresolvedVariables is set to false and any variable cannot be resolved, then the  policy throws a fault. 
If it is set to true and any variable is unresolvable, the variable is treated as empty string (Null).
Valid values: true/false
Default value: false
Copy (Optional) source
Copies the specified information from the <source> to the variable specified in the <AssignTo> element.
  • If the source is not specified, it is treated as a simple message.
  • If the source variable can not be resolved, or resolves to a non-message type, <Copy> fails to respond.
Headers Copies HTTP headers. To copy all headers, specify <Copy><Headers/></Copy>
QueryParams Copies query parameters. Note that the QueryParams is copied only when both the source and AssignTo type are request. To copy all query parameters, specify <Copy><QueryParams/></Copy>.
FormParams Copies the form parameters. Note that the FormParams is copied only when the contentType is source and AssignTo is application/x-www-form-urlencoded. To copy all form parameters, specify <Copy><FormParams/></Copy>.
Payload Valid values: true/false. If true, the Content-Type header is copied.
Version Valid values: true/false. If true, the HTTP version is copied.
Verb Valid values: true/false. If true, it is assigned only when both the source and AssignTo type are request.
Path
StatusCode Valid values: true/false. If true, it is assigned only when both the source and AssignTo type are response.
ReasonPhrase
Remove (Optional) Headers Removes HTTP headers from the variable specified in the <AssignTo> element. To remove all the headers, specify <Remove><Headers/></Remove>
QueryParams Removes the query parameters. Note that the QueryParams are removed only when the AssignTo type is request. To copy all query paremeters, specify <Copy><QueryParams/></Copy>.
FormParams Removes the form parameters. Note that FormParams are removed only when the contentType of AssignTo is application/x-www-form-urlencoded.To copy all form parameters, specify <Copy><FormParams/></Copy>.
Payload Valid values: true/false. If true, the payload is cleared.
Add (Optional) Headers Adds the headers in the variable specified in the <AssignTo> element. Note that the empty header <Add><Headers/></Add> does not add any header. The same holds true for QueryParams and FormParams.
QueryParams Adds the query parameters.
FormParams Adds the form parameters and the contentType of message is changed to application/x-www-form-urlencoded.
Set (Optional) Headers Sets/overwrites the HTTP headers in the variable specified in the <AssignTo> element. Note that the empty header <Set><Headers/></Set> does not set any header. The same holds true for QueryParams and FormParams.
QueryParams Sets/overwrites the query parameters.
FormParams Sets/overwrites the form parameters and the contentType of message changes to application/x-www-form-urlencoded.
Payload You are allowed to set mixed payload contents. For example, <root1>sunday</root1><root2>funday</root2><root3>{var1}</root3>.
  contentType If contentType is specified, its value is treated as message template, and assigned to the contentType header.
Version Sets the version. Note that the content of <Version> is treated as message template. The same holds true for Verb, Path, StatusCode, and ReasonPhrase.
Verb Sets the HTTP verb and path only when AssignTo type is request.
Path
StatusCode Sets the status code and reason phrase only when AssignTo type is response.
ReasonPhrase
AssignVariable (Optional) Name Specifies the name of the variable.
Ref Reference that assigns value to the variable.
Value If <Ref> is not specified or is unresolvable, this value is assigned to the variable.

Policy schema

Each policy must conform to a policy schema. All policy constructs such as elements and attributes mentioned above are defined in a schema. To download the schema, click here.

Example - AssignMessage policy

<AssignMessage name="create1">
<AssignTo type="[request]/response" createNew="[true]/false">sc.request</AssignTo>
<IgnoreUnresolvedVariables>true/[false]</IgnoreUnresolvedVariables>

// allow any order
<Copy source="request"> // if no children do copy all
    <Headers>
        <Header name="h1"/>
        <Header name="h2"/>
    </Headers>
    <QueryParams>
        <QueryParam name="q1"/>
        <QueryParam name="q2"/>
    </QueryParams>
    <FormParams>
        <FormParam name="f1"/>
        <FormParam name="f2"/>
    </FormParams>
    <Payload>true/[false]</Payload>
    <Verb>true/[false]</Verb>
    <Path>true/[false]</Path>
</Copy>

<Remove> // if no children do remove all
    <Headers>
        <Header name="h1"/>
        <Header name="h2"/>
    </Headers>
    <QueryParams>
        <QueryParam name="q1"/>
        <QueryParam name="q2"/>
    </QueryParams>
    <FormParams>
        <FormParam name="f1"/>
        <FormParam name="f2"/>
    </FormParams>
    <Payload>true</Payload>
</Remove>

<Add>
    <Headers>
        <Header name="h1">my{variable.v1}</Header>
    </Headers>
    <QueryParams>
        <QueryParam name="q1">my{variable.v1}</QueryParam>
    </QueryParams>
    <FormParams>
        <FormParam name="f1">my{variable.v1}</FormParam>
    </FormParams>
</Add>

<Set>
    <Headers>
        <Header name="h1">my{variable.v1}</Header>
    </Headers>
    <QueryParams>
        <QueryParam name="q1">my{variable.v1}</QueryParam>
    </QueryParams>
    <FormParams>
        <FormParam name="f1">my{variable.v1}</FormParam>
    </FormParams>

    <Payload contentType="text/xml">
        <root>this is my payload {variable.v1}</root>
    </Payload>

    <Verb>${variable.v1}</Verb>
    <Path>abc/def/{varaiable.v1}/d</Path>
</Set>

<AssignVariable>
    <Name>response.header.xyz</Name>
    <Ref>request.queryparam.abc</Ref>
    <Value>hello!</Value>
</AssignVariable>
</AssignMessage>