AssignMessage policy Stay organized with collections Save and categorize content based on your preferences.
This pageapplies toApigee andApigee hybrid.
View Apigee Edge documentation.![]()
The AssignMessage policy changes an existing request or response message, or creates a custom request or response message, during the API proxy flow.
You can use the AssignMessage policy to accomplish many different types of message changes, including to:
- Define a default value for a query string parameter or other type of input.
- Strip HTTP headers from a request message before the message is forwarded to the backend service.
- Add HTTP headers before a response is sent to the consumer app.
- Insert JSON or XML message content before a response is sent.
- Generate complete request or response messages. For example, you might use aServiceCallout policy to invoke a remote API from an API proxy. You can use the AssignMessage policy to create a custom request message and assign it to a variable. Then, use
ServiceCalloutto send that message to the remote API. - Add headers to requests and responses for debugging and troubleshooting.
To implement this policy, configure actions such as:
- Add new form parameters, headers, or query parameters to a message
- Copy existing properties from one message to another
- Remove headers, query parameters, form parameters, and message payloads from a message
- Set the value of properties in a message, including setting arbitrary context variables.
- Createcustom request messages and pass them to an alternative target
The AssignMessage policy executes actions in the order in which they appear in the policy configuration. If, for example, you need to remove all headers and then set a specific header, include the<Remove> element before the<Set> element.
This policy is anExtensible policy and use of this policy might have cost or utilization implications, depending on your Apigee license. For information on policy types and usage implications, seePolicy types.
<AssignMessage> element
Defines an AssignMessage policy.
| Default Value | SeeDefault Policy tab, below |
| Required? | Required |
| Type | Complex object |
| Parent Element | N/A |
| Child Elements | <Add><AssignTo><AssignVariable><Copy><DisplayName><IgnoreUnresolvedVariables><Remove><Set> |
The<AssignMessage> element uses the following syntax:
Syntax
The<AssignMessage> element uses the following syntax:
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><!--AllAssignMessagechildelementsareoptional--><Add><FormParams><FormParamname="FORMPARAM_NAME">FORMPARAM_VALUE</FormParam>...</FormParams><Headers><Headername="HEADER_NAME">HEADER_VALUE</Header>...</Headers><QueryParams><QueryParamname="QUERYPARAM_NAME">QUERYPARAM_VALUE</QueryParam>...</QueryParams></Add><AssignTocreateNew="[true|false]"transport="http"type="[request|response]">DESTINATION_VARIABLE_NAME</AssignTo><AssignVariable><Name>VARIABLE_NAME</Name><PropertySetRef>SOURCE_VARIABLE</PropertySetRef><Ref>SOURCE_VARIABLE</Ref><ResourceURL>RESOURCE_URL_OR_TEMPLATE</ResourceURL><Template>MESSAGE_TEMPLATE</Template>or<Templateref='TEMPLATE_VARIABLE'></Template><Value>VARIABLE_VALUE</Value></AssignVariable><Copysource="VARIABLE_NAME"><!--Canalsobeanemptyarray(<FormParams/>)--><FormParams><FormParamname="FORMPARAM_NAME">FORMPARAM_VALUE</FormParam>...</FormParams><!--Copyallheaders--><Headers/><!--or,copyspecificheadersbyname--><Headers><Headername="HEADER_NAME"/><!--or--><Headername="HEADER_NAME">[false|true]</Header>...</Headers><Path>[false|true]</Path><Payload>[false|true]</Payload><!--Canalsobeanemptyarray(<QueryParams/>)--><QueryParams><QueryParamname="QUERYPARAM_NAME">QUERYPARAM_VALUE</QueryParam>...</QueryParams><StatusCode>[false|true]</StatusCode><Verb>[false|true]</Verb><Version>[false|true]</Version></Copy><DisplayName>POLICY_DISPLAY_NAME</DisplayName><IgnoreUnresolvedVariables>[true|false]</IgnoreUnresolvedVariables><!--Canalsobeemptytoremoveeverythingfromthemessage(<Remove/>)--><Remove><!--Removeallformparameters--><FormParams/><!--or,removespecificformparametersbyname--><FormParams><FormParamname="FORMPARAM_NAME"/><!--or--><FormParamname="FORMPARAM_NAME">[false|true]</FormParam>...</FormParams><!--Removeallheaders--><Headers/><!--or,removespecificheadersbyname--><Headers><Headername="HEADER_NAME"/><!--or--><Headername="HEADER_NAME">[false|true]</Header>...</Headers><Payload>[false|true]</Payload><!--Removeallqueryparameters--><QueryParams/><!--or,removespecificqueryparametersbyname--><QueryParams><QueryParamname="QUERYPARAM_NAME"/><!--or--><QueryParamname="QUERYPARAM_NAME">[false|true]</QueryParam>...</QueryParams></Remove><Set><Authentication><HeaderName>HEADER_NAME</HeaderName><!--UseeitherGoogleAccessTokenorGoogleIDToken--><GoogleAccessToken><Scopes><Scope>SCOPE</Scope>...</Scopes></GoogleAccessToken>-----or-----<GoogleIDToken><Audienceref='FLOW_VARIABLE_NAME>TARGET_URL</Scope></GoogleAccessToken></Authentication><FormParams><FormParamname="FORMPARAM_NAME">FORMPARAM_VALUE</FormParam>...</FormParams><Headers><Headername="HEADER_NAME">HEADER_VALUE</Header>...</Headers><Path>PATH</Path><PayloadcontentType="CONTENT_TYPE"variablePrefix="PREFIX"variableSuffix="SUFFIX">NEW_PAYLOAD</Payload><QueryParams><QueryParamname="QUERYPARAM_NAME">QUERYPARAM_VALUE</QueryParam>...</QueryParams><StatusCode>HTTP_STATUS_CODEor{variable}</StatusCode><Verb>[GET|POST|PUT|PATCH|DELETE|{variable}]</Verb><Version>[1.0|1.1|{variable}]</Verb></Set></AssignMessage>
Default Policy
The following example shows the default settings when you add an AssignMessage policy to your flow in the Apigee UI. You will probably never want all of the configuration elements shown here.
<AssignMessagecontinueOnError="false"enabled="true"name="assign-message-default"><DisplayName>AssignMessage-1</DisplayName><Properties/><Copysource="request"><Headers/><QueryParams/><FormParams/><Payload/><Verb/><StatusCode/><Path/></Copy><Remove><Headers><Headername="h1"/></Headers><QueryParams><QueryParamname="q1"/></QueryParams><FormParams><FormParamname="f1"/></FormParams><Payload/></Remove><Add><Headers/><QueryParams/><FormParams/></Add><Set><Headers/><QueryParams/><FormParams/><!--<Verb>GET</Verb>--><Path/></Set><AssignVariable><Name>name</Name><Value/><Ref/></AssignVariable><IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables><AssignTocreateNew="false"transport="http"type="request"/></AssignMessage>
When you insert a new AssignMessage policy in the Apigee UI, the template contains stubs for all possible operations. Typically, you select which operations you want to perform with this policy and remove the rest of the child elements. For example, if you want to perform a copy operation, use the<Copy> element and remove<Add>,<Remove>, and other child elements from the policy to make it more readable.
This element has the following attributes that are common to all policies:
| Attribute | Default | Required? | Description |
|---|---|---|---|
name | N/A | Required | The internal name of the policy. The value of the Optionally, use the |
continueOnError | false | Optional | Set tofalse to return an error when a policy fails. This is expected behavior for most policies. Set totrue to have flow execution continue even after a policy fails. See also: |
enabled | true | Optional | Set totrue to enforce the policy. Set tofalse toturn off the policy. The policy will not be enforced even if it remains attached to a flow. |
async | false | Deprecated | This attribute is deprecated. |
The following table provides a high-level description of the child elements of<AssignMessage>:
| Child Element | Required? | Description |
|---|---|---|
| Common operations | ||
<Add> | Optional | Adds informationto the message object that is specified by the<AssignTo> element.
To overwrite existing headers or parameters, use the |
<Copy> | Optional | Copies informationfrom the message specified by thesource attributeto the message object specified by the<AssignTo> element. |
<Remove> | Optional | Deletes the specified elements from the message variable specified in the<AssignTo> element. |
<Set> | Optional | Replaces values of existing properties on the request or response, which is specified by the<AssignTo> element.
|
| Other child elements | ||
<AssignTo> | Optional | Specifies which message the AssignMessage policy operates on. This can be the standard request or response, or it can be a new custom message. |
<AssignVariable> | Optional | Assigns a value to a flow variable. If the variable does not exist, then<AssignVariable> creates it. |
<IgnoreUnresolvedVariables> | Optional | Determines whether processing stops when an unresolved variable is encountered. |
Each of these child elements is described in the sections that follow.
Examples
The following examples show some of the ways in which you can use the AssignMessage policy:
1: Add header
The following example adds a header to the request with the<Add> element:
<AssignMessage name="AM-add-headers-1"> <Add> <Headers> <Header name="partner-id">{verifyapikey.VAK-1.developer.app.partner-id}</Header> </Headers> </Add> <AssignTo>request</AssignTo></AssignMessage>2: Remove payload
The following example deletes the payload from the response with the<Remove> element:
<AssignMessagename="AM-remove-1"><DisplayName>remove-1</DisplayName><Remove><Payload>true</Payload></Remove><AssignTo>response</AssignTo></AssignMessage>
3: Modify response
The following example modifies an existing response object by adding a header to it:
<AssignMessage name="AM-modify-response"> <Set> <Headers> <Header name="Cache-Hit">{lookupcache.LookupCache-1.cachehit}</Header> </Headers> </Set> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> <AssignTo>response</AssignTo></AssignMessage>This example does not create a new message. Instead, it modifies an existing response message by adding an HTTP header.
Because this example specifiesresponse as the variable name in the<AssignTo> element, this policy modifies the response object that was originally set with data returned by the target server.
The HTTP header added to the response message by this policy is derived from a variable populated by theLookupCache policy. Therefore the response message modified by this Assign Message policy contains an HTTP header that indicates whether the results have been pulled from the cache or not. Setting headers in the response can be handy for debugging and troubleshooting.
Tip: This pattern of setting headers for debugging and troubleshooting is a common practice. You can see what header value was sent back by inspecting the response with the Debug tool or other means.4: Set dynamic content
You can use AssignMessage to embeddynamic content in the payload of response and request messages.
To embed flow variables in an XML payload, wrap the designated variable in curly braces, like this:{prefix.name}.
The following example embeds the value of theuser-agent HTTP header flow variable in an XML element calledUser-agent:
<AssignMessagename="AM-set-dynamic-content"><AssignTo>response</AssignTo><Set><PayloadcontentType="text/xml"><User-agent>{request.header.user-agent}</User-agent></Payload></Set><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables></AssignMessage>
For JSON payloads, you can insert variables using thevariablePrefix andvariableSuffix attributes with delimiter characters as shown in the following example:
<AssignMessagename="AM-set-payload"><PayloadcontentType="application/json"variablePrefix="@"variableSuffix="#">{"user-agent":"@request.header.user-agent#"}</Payload></AssignMessage>
For a complete list of flow variables, seeFlow variables reference.
You can also use curly braces to insert variables.
5: Remove query param
The following example removes theapikey query parameter from the request:
<AssignMessage name="AM-remove-query-param"> <Remove> <QueryParams> <QueryParam name="apikey"/> </QueryParams> </Remove> <AssignTo>request</AssignTo></AssignMessage>
It's a best practice to strip theapikey query parameter from the request message when you use theVerifyAPIKey policy for user authentication. You do this to prevent sensitive key information from being passed to the backend target.
6: Set/get variables
The following example uses three AssignMessage policies:
- Creates three flow variables in the request, with static values
- Gets the flow variables dynamically in a second policy in the request flow
- Sets them in the payload of the response
<!--Policy#1: Set variables in the request --><AssignMessagename="AM-set-variables"><!--CreateavariablenamedmyAppSecret--><AssignVariable><Name>myAppSecret</Name><Value>42</Value></AssignVariable><!--Createavariablenamedconfig.environment--><AssignVariable><Name>config.environment</Name><Value>test</Value></AssignVariable><!--Createavariablenamedconfig.protocol--><AssignVariable><Name>config.protocol</Name><Value>gopher</Value></AssignVariable></AssignMessage>
In the first policy, the<AssignVariable> element creates and sets three variables in the request. Each<Name> element specifies a variable name, and<Value> specifies the value.
The second policy uses the<AssignVariable> element to read in the values and creates three new variables:
<!--Policy#2: Get variables from the request --><AssignMessagecontinueOnError="false"enabled="true"name="get-variables"><AssignTocreateNew="false"transport="http"type="request"/><!--GetthevalueofmyAppSecretandcreateanewvariable,secret--><AssignVariable><Name>secret</Name><Ref>myAppSecret</Ref><Value>0</Value></AssignVariable><!--Getthevalueofconfig.environmentandcreateanewvariable,environment--><AssignVariable><Name>environment</Name><Ref>config.environment</Ref><Value>default</Value></AssignVariable><!--Getthevalueofconfig.protocolandcreateanewvariable,protocol--><AssignVariable><Name>protocol</Name><Ref>config.protocol</Ref><Value>default</Value></AssignVariable><IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables></AssignMessage>
In the second policy, the<Ref> element references the source variable, and<Name> elements specify the names of the new variables. If the variable referenced by the<Ref> element is not accessible, you can use the value specified by the<Value> element.
To try out this set of policies:
- Add policies #1 and #2 to the request flow. Be sure to put policy #1before policy #2.
- Add the third policy in theresponse flow.
- The third policy uses the
<Set>element to add the variables to the response. The following example constructs an XML payload in the response that Edge returns to the client:<!--Policy#3: Add variables to the response --><AssignMessagecontinueOnError="false"enabled="true"name="put-em-in-the-payload"><DisplayName>put-em-in-the-payload</DisplayName><Set><PayloadcontentType="application/xml"><wrapper><secret>{secret}</secret><config><environment>{environment}</environment><protocol>{protocol}</protocol></config></wrapper></Payload></Set><IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables><AssignTocreateNew="false"transport="http"type="response"/></AssignMessage>
Note that the syntax to access flow variables in
<Set>is to wrap them in curly braces.Be sure to set the
<Payload>element'scontentTypeattribute toapplication/xml. - Send a request to your API proxy; for example:
curl -vL https://ahamilton-eval-test.apigee.net/myproxy
Optionally, you can pipe the results through a utility such as
xmllintso that the XML is displayed in a nicely formatted structure:curl -vL https://ahamilton-eval-test.apigee.net/myproxy| xmllint --format -
The body of the response should look like the following:
42 test gopher
7: Get ServiceCallout response headers
In the following example, let's say that aServiceCallout policy is in the API proxy request, and the callout response contains multiple headers of the same name (Set-Cookie). Assuming the Service Callout's response variable is the defaultcalloutResponse, the following policy gets the secondSet-Cookie header value.
<AssignMessagename="AM-Payload-from-SC-header"><Set><PayloadcontentType="application/json">{"Cookies from Service Callout":" {calloutResponse.header.Set-Cookie.2}"}</Payload></Set><IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables><AssignTo>response</AssignTo></AssignMessage>
To listall header values, use the following variable instead:
{calloutResponse.header.Set-Cookie.values}8: Store and remove form params, headers, query params
If you want to use<Remove> to delete your headers, query parameters, or form parameters, but retain access to their values later in the policy flow, you can store their values using<AssignVariable>.
<AssignMessageasync="false"continueOnError="false"enabled="true"name="AM-StoreAndRemove"><DisplayName>AM-StoreAndRemove</DisplayName><AssignVariable><Name>var_grant_type</Name><Ref>request.formparam.grant_type</Ref></AssignVariable><Remove><Headers/><FormParams/><Payload/></Remove><Set><Headers><Headername="Content-Type">application/x-www-form-urlencoded</Header><Headername="Accept">application/json</Header><Headername="Grant-Type">{var_grant_type}</Header></Headers></Set><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables><AssignTocreateNew="false"transport="http"type="request"/></AssignMessage>
<Remove> in the Assign Message policy cannot be accessed after completion of the policy flow unless their values are stored in variables as described in this example.Each child element in this reference has additional examples. For even more examples, seeAssignMessage example on GitHub.
Child element reference
This section describes the child elements of<AssignMessage>.
<Add>
Adds information to the request or response, which is specified by the<AssignTo> element.
The<Add> element adds new properties on the message that don't exist in the original message. Note that<Set> also provides this functionality. To change the values ofexisting properties, use the<Set> element.
| Default Value | N/A |
| Required? | Optional |
| Type | Complex type |
| Parent Element | <AssignMessage> |
| Child Elements | <FormParams><Headers><QueryParams> |
The<Add> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Add> <FormParams> <FormParam name="FORMPARAM_NAME">FORMPARAM_VALUE</FormParam> ... </FormParams> <Headers> <Header name="HEADER_NAME">HEADER_VALUE</Header> ... </Headers> <QueryParams> <QueryParam name="QUERYPARAM_NAME">QUERYPARAM_VALUE</QueryParam> ... </QueryParams> </Add></AssignMessage>
Example 1
The following example modifies the request message by getting the values of three query string parameters from the initial request and setting them as form parameters on the target endpoint request, and then finally removing all of the original query string parameters:
<AssignMessage name="AM-add-formparams-3"> <Add> <FormParams> <FormParam name="username">{request.queryparam.name}</FormParam> <FormParam name="zip_code">{request.queryparam.zipCode}</FormParam> <FormParam name="default_language">{request.queryparam.lang}</FormParam> </FormParams> </Add> <Remove> <QueryParams/> </Remove> <AssignTo>request</AssignTo></AssignMessage>Example 2
The following example uses the<Headers> element to add apartner-id header to the request that will be sent to the target endpoint:
<AssignMessage name="AM-add-headers-1"> <Add> <Headers> <Header name="partner-id">{verifyapikey.VAK-1.developer.app.partner-id}</Header> </Headers> </Add> <AssignTo>request</AssignTo></AssignMessage>Example 3
The following example uses the<QueryParams> element to add a single query parameter with a static value to the request:
<AssignMessage name="AM-add-queryparams-1"> <Add> <QueryParams> <QueryParam name="myParam">42</QueryParam> </QueryParams> </Add> <AssignTo>request</AssignTo></AssignMessage>
This example uses<Add> in the request preflow. If you look at the results in a tool such as theDebug overview, the request tohttps://example-target.com/get becomeshttps://example-target.com/get?myParam=42.
The child elements of<Add> support dynamic string substitution, known asmessage templating.
<FormParams> (child of<Add>)
Adds new form parameters to the request message. This element has no effect on a response message.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<FormParam> elements |
| Parent Element | <Add> |
| Child Elements | <FormParam> |
The<FormParams> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Add><FormParams><FormParamname="FORMPARAM_NAME">FORMPARAM_VALUE</FormParam>...</FormParams><AssignTocreateNew="[true|false]"transport="http"type="[request|response]">DESTINATION_VARIABLE_NAME</AssignTo></Add></AssignMessage>
Example 1
The following example adds a single form parameter (answer) and a static value (42) to the request:
<AssignMessage name="AM-add-formparams-1"> <Add> <FormParams> <FormParam name="answer">42</FormParam> </FormParams> </Add> <AssignTo>request</AssignTo></AssignMessage>
Example 2
The following example gets the value of thename query parameter and adds it to the request as a form parameter, and then removes the query parameter:
<AssignMessage name="AM-Swap-QueryParam-to-FormParams"> <Add> <FormParam name="name">{request.queryparam.name}</FormParam> </Add> <Remove> <QueryParam name="name"/> </Remove></AssignMessage>Note that this example does not specify a target with<AssignTo>. This policy adds the parameter to the request only.
Example 3
The following example adds multiple form parameters to the request:
<AssignMessage name="AM-add-formparams-3"> <Add> <FormParams> <FormParam name="username">{request.queryparam.name}</FormParam> <FormParam name="zip_code">{request.queryparam.zipCode}</FormParam> <FormParam name="default_language">{request.queryparam.lang}</FormParam> </FormParams> </Add> <Remove> <QueryParams/> </Remove> <AssignTo>request</AssignTo></AssignMessage>This example gets the query string parameters from the originating request and adds them as form parameters with different names. Then it removes the original query parameters. Apigee will send the modified request to the target endpoint.
You can use theDebug overview to look at the flow. You'll see that the body of the request contains the URL-encoded form data, which was originally passed in as query string parameters:
username=nick&zip_code=90210&default_language=en
You can use<FormParams> only when the following criteria are met:
- HTTP verb: POST
- Message type: Request
- One (or both) of the following:
- Form data: Set to some value, or "" (the empty string). For example, with
curl, add-d ""to your request. Content-Lengthheader: Set to 0 (if no data is in the original request; otherwise, the current length, in bytes). For example, withcurladd-H "Content-Length: 0"to your request.
- Form data: Set to some value, or "" (the empty string). For example, with
For example:
curl -vL -X POST -d "" -H "Content-Type: application/x-www-form-urlencoded" https://ahamilton-eval-test.apigee.net/am-test
When you add<FormParams>, Apigee sets the request'sContent-Type header toapplication/x-www-form-urlencoded before sending the message to the target service.
<Headers> (child of<Add>)
Adds new headers to the specified request or response, which is specified by the<AssignTo> element.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<Header> elements |
| Parent Element | <Add> |
| Child Elements | <Header> |
The<Headers> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Add> <Headers> <Header name="HEADER_NAME">HEADER_VALUE</Header> ... </Headers> </Add></AssignMessage>
Example 1
The following example adds apartner-id header to the request message, and assigns the value of theverifyapikey.VAK-1.developer.app.partner-id flow variable to that header.
<AssignMessage name="AM-add-headers-1"> <Add> <Headers> <Header name="partner-id">{verifyapikey.VAK-1.developer.app.partner-id}</Header> </Headers> </Add> <AssignTo>request</AssignTo></AssignMessage><QueryParams> (child of<Add>)
Adds new query parameters to the request. This element has no effect on a response.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<QueryParam> elements |
| Parent Element | <Add> |
| Child Elements | <QueryParam> |
The<QueryParams> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Add> <QueryParams> <QueryParam name="QUERYPARAM_NAME">QUERYPARAM_VALUE</QueryParam> ... </QueryParams> </Add></AssignMessage>
Example 1
The following example adds the query parametermyParam to the request and assigns the value42 to it:
<AssignMessage name="AM-add-queryparams-1"> <Add> <QueryParams> <QueryParam name="myParam">42</QueryParam> </QueryParams> </Add> <AssignTo>request</AssignTo></AssignMessage>
You can use<QueryParams> only when the following criteria are met:
- HTTP verbs:
GET,POST,PATCH,DELETE - Message type: Request
In addition, you can only set query parameters when the<AssignTo> element'stype attribute is a request message. Setting them on the response has no effect.
If you define an empty array of query parameters in your policy (<Add><QueryParams/></Add>), the policy does not add any query parameters. This is the same as omitting<QueryParams>.
<AssignTo>
Determines which object the AssignMessage policy operates on. The options are:
- Request message: The
requestreceived by the API proxy - Response message: The
responsereturned from the target server - Custom message: A custom request or response object
Note that in some cases, you cannot change the object on which the AssignMessage policy acts. For example, you cannot use<Add> or<Set> to add or change query parameters (<QueryParams>) or form parameters (<FormParams>) on the response. You can only manipulate query parameters and form parameters on the request.
| Default Value | N/A |
| Required? | Optional |
| Type | String |
| Parent Element | <AssignMessage> |
| Child Elements | None |
If you do not specify<AssignTo>, or if you specify the<AssignTo> element, but do not specify a text value for the element, the policy acts on the default request or response, which is based on where the policy executes. If the policy executes in the request flow, it affects the request message. If it executes in the response flow, the policy affects the response by default.
The<AssignTo> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><AssignTocreateNew="[true|false]"transport="http"type="[request|response]">DESTINATION_VARIABLE_NAME</AssignTo></AssignMessage>
Example 1
The following example specifies no message in the text of the<AssignTo>. That implies that the policy will act on either therequest orresponse message, depending on where the policy executes.
<AssignMessagename="assignto-1"><AssignTocreateNew="false"transport="http"type="request"/><!--no-op-->...</AssignMessage>
If you specifycreateNew="false", and do not explicitly provide a message name, the other attributes of<AssignTo> are irrelevant. In this case, you might wish to omit the<AssignTo> element completely.
Example 2
The following example creates a new request object, over-writing the existing object:
<AssignMessagename="assignto-2"><AssignTocreateNew="true"transport="http"type="request"/>...</AssignMessage>
When you create a new request or response object, the other elements of the AssignMessage policy (such as<Add>,<Set>, and<Copy>) act on that new request or response object.
You can access the new request object in other policies later in the flow, or send the new request object to an external service with aServiceCallout policy.
Example 3
The following example creates a new request object namedMyRequestObject:
<AssignMessagename="assignto-2"><AssignTocreateNew="true"transport="http"type="request">MyRequestObject</AssignTo>...</AssignMessage>
When you create a new request or response object, the other elements of the AssignMessage policy (such as<Add>,<Set>, and<Copy>) act on that new request object.
You can access the new request object by name in other policies later in the flow, or send the new request object to an external service with aServiceCallout policy.
The following table describes the attributes of<AssignTo>:
<AssignVariable>
Assigns a value to a destination flow variable (such as a variable whose value is set by the AssignMessage policy). If the flow variable does not exist, then<AssignVariable> creates it. You can use multiple AssignVariable elements within the AssignMessage policy. They are executed in order of appearance in the policy configuration.
| Default Value | N/A |
| Required? | Optional |
| Type | Complex type |
| Parent Element | <AssignMessage> |
| Child Elements | <Name>(required)<PropertySetRef><Ref><ResourceURL><Template><Value> |
The value that you assign to the destination flow variable can be one of the following:
- Literal string: Use the
<Value>child element to specify a literal string value for the destination flow variable. - Flow variable: Use the
<Ref>child element to specify the value of an existing flow variable for the destination flow variable. For a complete list of flow variables that can be used as a source, seeFlow variables reference. - Property set: Use the
<PropertySetRef>child element to retrieve the value from aproperty set name/key pair and store it in a flow variable. Lets you access property sets dynamically. - Resource URL: Use the
<ResourceURL>child element to specify a URL for a text resource, of type XSL, XSD, WSDL, JavaScript, or OpenAPI Spec. This assigns the contents of the resource into the named flow variable. - Message template: Use the
<Template>child element to specify amessage template for the destination flow variable.
The order of precedence for these child elements is: ResourceURL, Template, Ref, Value, PropertySetRef.
The<AssignVariable> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <AssignVariable> <Name>VARIABLE_NAME</Name> <PropertySetRef>SOURCE_VARIABLE</PropertySetRef> <Ref>SOURCE_VARIABLE</Ref> <ResourceURL>RESOURCE_URL_OR_TEMPLATE</ResourceURL> <Template>MESSAGE_TEMPLATE</Template> or <Template ref='TEMPLATE_VARIABLE'></Template> <Value>VARIABLE_VALUE</Value> </AssignVariable></AssignMessage>
Use the<Ref> element to specify the source variable. If the variable referenced by<Ref> is not accessible, Apigee uses the value specified by the<Value> element. If you define<Template>, it takes precedence over the<Ref> and<Value> sibling elements.
Example 1
The following example sets the value of a new variable,myvar, to the literal value42:
<AssignMessagename="assignvariable-1"><AssignVariable><Name>myvar</Name><Value>42</Value></AssignVariable></AssignMessage>
Example 2
The following example assigns the value of the flow variablerequest.header.user-agent to the destination flow variablemyvar and the value of the query parametercountry to the destination flow variableCountry:
<AssignMessagename="assignvariable-2"><AssignVariable><Name>myvar</Name><Ref>request.header.user-agent</Ref><Value>ErrorOnCopy</Value></AssignVariable><AssignVariable><Name>Country</Name><Ref>request.queryparam.country</Ref><Value>ErrorOnCopy</Value></AssignVariable></AssignMessage>
If either assignment fails, Apigee instead assigns the valueErrorOnCopy to the destination flow variable.
If themyvar orCountry flow variables do not exist,<AssignVariable> creates them.
Example 3
The following example uses the<Template> child element to concatenate two context variables with a literal string (a hyphen) between them:
<AssignMessagename='AV-via-template-1'><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables><AssignVariable><Name>my_destination_variable</Name><Value>BADDBEEF</Value><Template>{system.uuid}-{messageid}</Template></AssignVariable></AssignMessage>
Example 4
The following example uses<AssignVariable> to disable the default behavior of propagating the path suffix from the proxy request to the target request:
<AssignMessage name='AM-PathSuffixFalse'> <AssignVariable> <Name>target.copy.pathsuffix</Name> <Value>false</Value> </AssignVariable></AssignMessage>
A common use for<AssignVariable> is to set a default value for a query parameter, header, or other value that can be passed in with the request. You do this with a combination of both the<Ref> and<Value> child elements. For more information, see the examples for<Ref>.
<Name> (child of<AssignVariable>)
Specifies the name of the destination flow variable - the variable whose value is set by the AssignMessage policy. If the variable named in<Name> does not exist, the policy creates one with that name.
| Default Value | N/A |
| Required? | Required |
| Type | String |
| Parent Element | <AssignVariable> |
| Child Elements | None |
The<Name> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <AssignVariable> <Name>VARIABLE_NAME</Name> </AssignVariable></AssignMessage>
Example 1
The following example specifies the destination variable asmyvar, and sets it to the literal value42:
<AssignMessagename="assignvariable-1"><AssignVariable><Name>myvar</Name><Value>42</Value></AssignVariable></AssignMessage>
Ifmyvar does not exist,<AssignVariable> creates it.
ratelimit is reserved. For more information, seeQuota Policy Throws NullPointerException in the Apigee Community.<PropertySetRef> (child of<AssignVariable>)
This element allows you to retrieve the value of a property set name/key pair dynamically. To learn aboutproperty sets, seeUsing property sets.
| Default Value | N/A |
| Required? | Optional |
| Type | String |
| Parent Element | <AssignVariable> |
| Child Elements | None |
A property set consists of a name/key pair. For example:propset1.id=12345, wherepropset1 is the name of the property set,id is a key, and12345 is the key's value.
ThePropertySetRef child element lets you select property set names and/or keys dynamically. Assume you have 200 routing rules in a property set file. You can access the property set rules as follows, whereroutingrules is the property set name andrule1,rule2,rulenare keys:
propertyset.routingrules.rule1propertyset.routingrules.rule2propertyset.routingrules.rulen
To access these properties in an API proxy flow, you must know which rule you want to select at design time. However, assume the rule name comes in the request header or payload. One way to select the rule is to use a JavaScript policy with code such as the following:
context.getVariables("propertyset.routingrules." + ruleName); //assuming ruleName was populated earlier. On the other hand, the AssignMessagePropertySetRef feature lets you select a property key dynamically without introducing JavaScript.
You can use a mix of flow variables and literal string values in the<PropertySetRef> element. See the examples for more details.
propertyset, as in:propertyset.PROPNAME.KEY. The prefixpropertyset is assumed.The<PropertySetRef> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <AssignVariable> <PropertySetRef>SOURCE_VARIABLE</PropertySetRef> </AssignVariable></AssignMessage>
Example 1
This example assigns the value from a property set key to a flow variable. In this case, the property set name is obtained from the headerpropset_name, the key is provided in the headerpropset_key, and the value assigned to the key is stored in the variableflow_variable.
<AssignMessageasync="false"continueOnError="false"enabled="true"name="assignMessage"><DisplayName>AssignMessage-1</DisplayName><Properties/><AssignVariable><Name>flow_variable</Name><PropertySetRef>{request.header.propset_name}.{request.header.propset_key}</PropertySetRef></AssignVariable></AssignMessage>
You can use any combination of flow variables and literal strings in the<PropertySetRef> element.
Example 2
This example assigns the value from a property set key to a flow variable using a fixed key name (literal string). In this case, the property set name is obtained from the headerpropset_name, the key is the literal stringkey1, and the value assigned to the key is stored in the variableflow_variable.
<AssignMessageasync="false"continueOnError="false"enabled="true"name="assignMessage"><DisplayName>AssignMessage-1</DisplayName><Properties/><AssignVariable><Name>flow_variable</Name><PropertySetRef>{request.header.propset_name}.key1</PropertySetRef></AssignVariable></AssignMessage>
You can use any combination of flow variables and literal strings in the<PropertySetRef> element.
<Ref> (child of<AssignVariable>)
Specifies the source of the assignment as a flow variable. The flow variable can be one of the pre-defined flow variables (as listed in theFlow variables reference), or a custom flow variable that you created.
The value of<Ref> is always interpreted as a flow variable; you cannot specify a literal string as the value. To assign a literal string value, use the<Value> element instead.
| Default Value | N/A |
| Required? | Optional |
| Type | String |
| Parent Element | <AssignVariable> |
| Child Elements | None |
When you specify a flow variable with<Ref>, omit the enclosing brackets{} that you would normally use to reference a flow variable. For example, to set the value of your new variable to the value of theclient.host flow variable:
DO specify the variable name without brackets:<Ref>client.host</Ref>DO NOT use brackets:<Ref>{client.host}</Ref>
To define a default value for the destination flow variable, use<Value> in combination with<Ref>. If the flow variable specified by<Ref> does not exist, cannot be read, or is null, then Apigee assigns the value of<Value> to the destination flow variable instead.
The<Ref> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <AssignVariable> <Name>VARIABLE_NAME</Name> <Ref>SOURCE_VARIABLE</Ref> </AssignVariable></AssignMessage>
Example 1
The following example assigns the value of the flow variablerequest.header.user-agent to the destination flow variablemyvar and the value of the query parametercountry to theCountry variable:
<AssignMessagename="assignvariable-4"><AssignVariable><Name>myvar</Name><Ref>request.header.user-agent</Ref></AssignVariable><AssignVariable><Name>Country</Name><Ref>request.queryparam.country</Ref></AssignVariable></AssignMessage>
In this example, Apigee does not have a default (or fallback value) specified for either assignment.
Example 2
The following example assigns the value of the flow variablerequest.header.user-agent to the destination flow variablemyvar and the value of the query parametercountry to theCountry variable:
<AssignMessagename="assignvariable-2"><AssignVariable><Name>myvar</Name><Ref>request.header.user-agent</Ref><Value>ErrorOnCopy</Value></AssignVariable><AssignVariable><Name>Country</Name><Ref>request.queryparam.country</Ref><Value>ErrorOnCopy</Value></AssignVariable></AssignMessage>
In this example, if the values of therequest.header.user-agent flow variable or theCountry query parameter are null, unreadable, or malformed, Apigee assigns the valueErrorOnCopy to the new variables.
Example 3
A common use case for<AssignVariable> is to set the default value of a query parameter, header, or other value that can be passed in with the request. For example, you create a weather API proxy where the request takes a single query parameter namedw. This parameter contains the ID of the city for which you want the weather. The request URL has the form:
http://myCO.com/v1/weather/forecastrss?w=CITY_ID
To define a default value forw, create an AssignMessage policy like the following:
<AssignMessagecontinueOnError="false"enabled="true"name="assignvariable-3"><AssignTocreateNew="false"transport="http"type="request"/><IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables><AssignVariable><Name>request.queryparam.w</Name><Ref>request.queryparam.w</Ref><Value>12797282</Value></AssignVariable></AssignMessage>
In this example,<AssignVariable> gets the value ofrequest.queryparam.w and assigns it to itself. If the flow variable is null, meaning thew query parameter was omitted from the request, then this example uses the default value from the<Value> element. Therefore, you can make a request to this API proxy that omits thew query parameter:
http://myCO.com/v1/weather/forecastrss
...and still have the API proxy return a valid result.
The value of<Ref> must be a flow variable, such as a property of arequest,response, ortarget object, or the name of a custom flow variable.
If you specify a flow variable that does not exist for the value of<Ref>, and the value of<IgnoreUnresolvedVariables> isfalse, Apigee throws an error.
<ResourceURL> (child of<AssignVariable>)
Specifies the URL of atext resource as the source of the variable assignment. Apigee loads the flow variable specified in<Name> with the contents of the referenced resource. The resource can be of type XSD, XSL, WSDL, JavaScript, Property Set, or OpenAPI Spec.
| Default Value | N/A |
| Required? | Optional |
| Type | String |
| Parent Element | <AssignVariable> |
| Child Elements | None |
If the resource specified by<ResourceURL> does not exist, then: if the value of<IgnoreUnresolvedVariables> istrue, Apigee assigns the valuenull to the destination flow variable, whereas if the value of<IgnoreUnresolvedVariables> isfalse, Apigee throws an error.
The<ResourceURL> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <AssignVariable> <Name>VARIABLE_NAME</Name> <ResourceURL>RESOURCE_URL_OR_TEMPLATE</ResourceURL> </AssignVariable></AssignMessage>
The text value takes a string value, and is interpreted as amessage template. Any of these are valid:
jsc://my-js-file.js wsdl://{variable-goes-here} {variable-goes-here}
Example 1
The following example assigns the value of a JSON resource, loaded into the proxy in thejsc folder, into the flow variableassigned-variable:
<AssignMessagename='AM-From-ResourceURL-Proxy-JSC'><AssignVariable><Name>assigned-variable</Name><ResourceURL>jsc://settings.json</ResourceURL></AssignVariable></AssignMessage>
Example 2
The following example assigns the value of an OpenAPI Spec resource, loaded into the proxy in theoas folder, into the flow variableassigned-variable, and then sets that value as thePayload in the response body:
<AssignMessagename='AM-Response'><AssignVariable><Name>assigned-variable</Name><ResourceURL>oas://Fulfillment.yaml</ResourceURL></AssignVariable><Set><PayloadcontentType='application/yaml'>{assigned-variable}</Payload></Set></AssignMessage>
<Template> (child of<AssignVariable>)
Specifies amessage template. A message template allows you to perform variable string substitution when the policy executes, and can combine literal strings with variable names wrapped in curly braces. In addition, message templates support functions such as escaping and case conversion.
Use theref attribute to specify a flow variable where the value of the variable is a message template. For example, you could store a message template as acustom attribute on a developer app. When Apigee identifies the developer app after it verifies the API key or security token (through an additional policy), the<AssignVariable> element could use the message template from the app's custom attribute, which is available as a flow variable from the security policy. The following example assumes the message template is available in a custom attribute calledmessage_template on the developer app making the API call, where theVerifyAPIKey policy was used to verify the app's API key:
<Template ref='verifyapikey.myVerifyAPIKeyPolicy.app.name.message_template'/>
| Default Value | N/A |
| Required? | Optional |
| Type | String |
| Parent Element | <AssignVariable> |
| Child Elements | None |
The<Template> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <AssignVariable> <Template>MESSAGE_TEMPLATE</Template> or <Template ref='TEMPLATE_VARIABLE'></Template> </AssignVariable></AssignMessage>
Example 1
The following example uses message templating syntax to concatenate two context variables with a literal string (a hyphen) between them:
<AssignMessagename='AV-via-template-1'><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables><AssignVariable><Name>my_destination_variable</Name><Value>BADDBEEF</Value><Template>{system.uuid}-{messageid}</Template></AssignVariable></AssignMessage>
Example 2
The following example specifies a flow variable, where the value of the variable is a pre-defined message template. Use this option if you want to inject a pre-defined template at runtime without having to modify the policy:
<AssignMessagename='AV-via-template-indirectly'><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables><AssignVariable><Name>my_destination_variable</Name><Value>BADDBEEF</Value><Templateref='my_template_variable'/></AssignVariable></AssignMessage>
Example 3
The following example specifies a flow variable and a text value. In this case, if the referenced variable is non-null, that value is used as the template. If the referenced value is null, then the text value (in this case,{system.uuid}-{messageid}) is used as the template. This pattern is useful to provide anoverride value, where in some cases you want to override the default template (the text part) with values that are set dynamically. For example, a conditional statement might grab a value from a key-value map and set the referenced variable to that value:
<AssignMessagename='AV-template-with-fallback'><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables><AssignVariable><Name>my_destination_variable</Name><Templateref='my_variable'>{system.uuid}-{messageid}</Template></AssignVariable></AssignMessage>
<Value> (child of<AssignVariable>)
Defines the value of the destination flow variable set with<AssignVariable>. The value is always interpreted as a literal string; you cannot use a flow variable as the value, even if you wrap the value in brackets ({}). To use a flow variable, use<Ref> instead.
| Default Value | N/A |
| Required? | Optional |
| Type | String |
| Parent Element | <AssignVariable> |
| Child Elements | None |
When used in combination with the<Ref> element,<Value> acts as the default (or fallback) value. If<Ref> is not specified, is unresolvable, or is null, the value of<Value> is used.
The<Value> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <AssignVariable> <Name>VARIABLE_NAME</Name> <Value>VARIABLE_VALUE</Value> </AssignVariable></AssignMessage>
Example 1
The following example sets the value of the destination flow variable,myvar, to the literal value42:
<AssignMessagename="assignvariable-1"><AssignVariable><Name>myvar</Name><Value>42</Value></AssignVariable></AssignMessage>
Example 2
The following example assigns the value of the flow variablerequest.header.user-agent to the flow variablemyvar and the value of the query parametercountry to theCountry variable:
<AssignMessagename="assignvariable-2"><AssignVariable><Name>myvar</Name><Ref>request.header.user-agent</Ref><Value>ErrorOnCopy</Value></AssignVariable><AssignVariable><Name>Country</Name><Ref>request.queryparam.country</Ref><Value>ErrorOnCopy</Value></AssignVariable></AssignMessage>
If either assignment fails,<AssignVariable> instead assigns the valueErrorOnCopy to the destination flow variable.
<Copy>
Copies valuesfrom the message specified by thesource attributeto the message specified by the<AssignTo> element. If you do not specify a target with<AssignTo>, then this policy copies the values to the request or the response, depending on where in the flow this policy executes.
| Default Value | N/A |
| Required? | Optional |
| Type | String |
| Parent Element | <AssignMessage> |
| Child Elements | <FormParams><Headers><Path><Payload><QueryParams><StatusCode><Verb><Version> |
If you specify no child elements beneath the<Copy> element, then it will copy all parts of the designated source message.
The<Copy> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Copysource="VARIABLE_NAME"><!--Canalsobeanemptyarray(<FormParams/>)--><FormParams><FormParamname="FORMPARAM_NAME">FORMPARAM_VALUE</FormParam>...</FormParams><!--Copyallheaders--><Headers/><!--or,copyspecificheadersbyname--><Headers><Headername="HEADER_NAME"/><!--or--><Headername="HEADER_NAME">[false|true]</Header>...</Headers><Path>[false|true]</Path><Payload>[false|true]</Payload><!--Canalsobeanemptyarray(<QueryParams/>)--><QueryParams><QueryParamname="QUERYPARAM_NAME">QUERYPARAM_VALUE</QueryParam>...</QueryParams><StatusCode>[false|true]</StatusCode><Verb>[false|true]</Verb><Version>[false|true]</Version></Copy><!--Usedasthedestinationforthe<Copy>values--><AssignTocreateNew="[true|false]"transport="http"type="[request|response]">DESTINATION_VARIABLE_NAME</AssignTo></AssignMessage>Example 1
The following example copies a header, three form parameters, the path, and all query parameters from therequest message to a new, custom request namednewRequest:
<AssignMessagename="AM-copy-1"><AssignTocreateNew="true"transport="http"type="request">newRequest</AssignTo><Copysource="request"><Headers><Headername="Header_Name_1"/></Headers><FormParams><FormParamname="Form_Param_Name_1"/><FormParamname="Form_Param_Name_2"/><FormParamname="Form_Param_Name_3"/></FormParams><Path>true</Path><QueryParams/></Copy></AssignMessage>
Because elements like<Payload> and<Verb> are not present, the policy does not copy those parts of the message.
Example 2
The following example first removes everything in the existingresponse message, then copies all the values from a different message calledsecondResponse into theresponse message:
<AssignMessage name='AM-Copy-Response'> <AssignTo>response</AssignTo> <!-- first remove any existing values --> <Remove/> <!-- then copy everything from the designated message --> <Copy source="secondResponse"/></AssignMessage>
The<Copy> element has a single attribute:
| Attribute | Description | Required? | Type |
|---|---|---|---|
| source | Specifies the source object of the copy.
| Optional | String |
<FormParams> (child of<Copy>)
Copies form parameters from the request specified by the<Copy> element'ssource attributeto the request specified by the<AssignTo> element. This element has no effect on a response.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<FormParam> elements or an empty array |
| Parent Element | <Copy> |
| Child Elements | <FormParam> |
The<FormParams> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Copysource="VARIABLE_NAME"><!--Canalsobeanemptyarray(<FormParams/>)--><FormParams><FormParamname="FORMPARAM_NAME">FORMPARAM_VALUE</FormParam>...</FormParams></Copy></AssignMessage>
Example 1
The following example copies a single form parameter from the request to the custom requestMyCustomRequest:
<AssignMessagename="AM-copy-formparams-1"><Copysource="request"><FormParams><FormParamname="paramName">Formparamvalue1</FormParam></FormParams></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
Example 2
The following example copies all form parameters to the custom requestMyCustomRequest:
<AssignMessagename="AM-copy-formparams-2"><Copysource="request"><FormParams/></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
Example 3
The following example copies three form parameters to the custom requestMyCustomRequest:
<AssignMessagename="AM-copy-formparams-3"><Copysource="request"><FormParams><FormParamname="paramName1"/><FormParamname="paramName2"/><FormParamname="paramName3"/></FormParams></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
Example 4
If there are multiple form params with the same name, use the following syntax:
<AssignMessagename="AM-copy-formparams-4"><Copysource="request"><FormParams><FormParamname="f1"/><FormParamname="f2"/><FormParamname="f3.2"/></FormParams></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
This example copiesf1,f2, and the second value off3. Iff3 has only one value, then it is not copied.
You can use<Copy>/<FormParams> only when the following criteria are met:
- The Message type for both source and destination is request
- HTTP verb for both source and destination is
POSTorPUT - The source message has the
Content-Typeheader set toapplication/x-www-form-urlencoded.
When you copy<FormParams>,<Copy> sets theContent-Type header in the destination message (designated by the<AssignTo> element) toapplication/x-www-form-urlencoded.
<Headers> (child of<Copy>)
Copies HTTP headersfrom the request or response message specified by the<Copy> element'ssource attributeto the request or response message specified by the<AssignTo> element.
| Default Value | N/A |
| Required? | Optional |
| Type | An array of<Header> elements or an empty array |
| Parent Element | <Copy> |
| Child Elements | <Header> |
The<Headers> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Copy source="VARIABLE_NAME"> <!-- Copy all headers --> <Headers/> <!-- or, copy specific headers by name --> <Headers> <Header name="HEADER_NAME"/> <!-- or --> <Header name="HEADER_NAME">[false|true]</Header> ... </Headers> </Copy></AssignMessage>
Example 1
The following example copies theuser-agent header from the request to the new, custom request object:
<AssignMessagename="AM-copy-headers-1"><Copysource="request"><Headers><Headername="user-agent"/></Headers></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
Example 2
To copy all headers, use an empty<Headers> element, as the following example shows:
<AssignMessagename="AM-copy-headers-2"><Copysource="request"><Headers/></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
Example 3
If there are multiple headers with the same name, use the following syntax:
<AssignMessagename="AM-copy-headers-3"><Copysource="request"><Headers><Headername="h1"/><Headername="h2"/><Headername="h3.2"/></Headers></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
This example copiesh1,h2, and the second value ofh3. Ifh3 has only one value, then it is not copied.
<Path> (child of<Copy>)
Determines whether the path should be copied from the source request to the destination request. This element has no effect on a response.
Iftrue, this policy copies the pathfrom the request message specified by the<Copy> element'ssource attributeto the request message specified by the<AssignTo> element.
| Default Value | False |
| Required? | Optional |
| Type | Boolean |
| Parent Element | <Copy> |
| Child Elements | None |
The<Path> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Copy source="VARIABLE_NAME"> <Path>[false|true]</Path> </Copy></AssignMessage>
Example 1
The following example indicates that AssignMessage should copy the path from the source request to the new, custom request object:
<AssignMessagename="copy-path-1"><Copysource="request"><Path>true</Path></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
You can use<Copy>/<Path> only when the following criteria are met:
- The Message type for both source and destination is request
<Payload> (child of<Copy>)
Determines whether the payload should be copied from the source to the destination. The source and destination can be requests or responses.
Iftrue, this policy copies the payloadfrom the message specified by the<Copy> element'ssource attributeto the message specified by the<AssignTo> element.
| Default Value | False |
| Required? | Optional |
| Type | Boolean |
| Parent Element | <Copy> |
| Child Elements | None |
The<Payload> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Copysource="VARIABLE_NAME"><Payload>[false|true]</Payload></Copy></AssignMessage>
Example 1
The following example sets<Payload> totrue so that the request payload is copied from the request to the response:
<AssignMessagename="AM-copy-payload-1"><Copysource="request"><Payload>true</Payload></Copy><AssignTo>response</AssignTo></AssignMessage>
<QueryParams> (child of<Copy>)
Copies query string parametersfrom the request specified by the<Copy> element'ssource attributeto the request specified by the<AssignTo> element. This element has no effect on a response.
| Default Value | N/A |
| Required? | Optional |
| Type | An array of<QueryParam> elements or an empty array |
| Parent Element | <QueryParam> |
| Child Elements | None |
The<QueryParams> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Copysource="VARIABLE_NAME"><!--Canalsobeanemptyarray(<QueryParams/>)--><QueryParams><QueryParamname="QUERYPARAM_NAME">QUERYPARAM_VALUE</QueryParam>...</QueryParams></Copy></AssignMessage>
Example 1
The following example copies themy_param query parameter from the request into a new, custom request object:
<AssignMessagename="copy-queryparams-1"><Copysource="request"><QueryParams><QueryParamname="my_param"/></QueryParams></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
Example 2
The following example copies all query parameters from the request into a new, custom request object:
<AssignMessagename="copy-queryparams-2"><Copysource="request"><QueryParams/></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
Example 3
If there are multiple query params with the same name, use the following syntax:
<AssignMessagename="copy-queryparams-3"><Copysource="request"><QueryParams><QueryParamname="qp1"/><QueryParamname="qp2"/><QueryParamname="qp3.2"/></QueryParams></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
This example copiesqp1,qp2, and the second value ofqp3. Ifqp3 has only one value, then it is not copied.
You can use<Copy>/<QueryParams> only when the following criteria are met:
- HTTP verbs:
GET,PUT,POST,PATCH,DELETE - The Message type for both source and destination is request
<StatusCode> (child of<Copy>)
Determines whether the status code is copied from the source response to the destination response. This element has no effect on a request.
Iftrue, this policy copies the status codefrom the response message specified by the<Copy> element'ssource attributeto the response message specified by the<AssignTo> element.
| Default Value | False |
| Required? | Optional |
| Type | Boolean |
| Parent Element | <Copy> |
| Child Elements | None |
The<StatusCode> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Copy source="VARIABLE_NAME"> <StatusCode>[false|true]</StatusCode> </Copy></AssignMessage>
Example 1
The following example sets<StatusCode> totrue, which copies the status code from the default response object to a new, custom response object:
<AssignMessagename="copy-statuscode-1"><Copysource="response"><StatusCode>true</StatusCode></Copy><AssignTocreateNew="true"transport="http"type="response">MyCustomResponse</AssignTo></AssignMessage>
You can use<StatusCode> only when the source and destination messages are of type response.
A common use of<StatusCode> is to set that the proxy response status code to a different value than that received from the target.
<Verb> (child of<Copy>)
Determines whether the HTTP verb is copied from the source request to the destination request. This element has no effect on a response.
Iftrue, copies the verb found in the<Copy> element'ssource attribute to the request specified in the<AssignTo> element.
| Default Value | False |
| Required? | Optional |
| Type | Boolean |
| Parent Element | <Copy> |
| Child Elements | None |
The<Verb> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Copy source="VARIABLE_NAME"> <Verb>[false|true]</Verb> </Copy></AssignMessage>
Example 1
The following example sets<Verb> totrue, which copies the verb from the default request to a new, custom request:
<AssignMessagename="copy-verb-1"><Copysource="request"><Verb>true</Verb></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
You can use<Copy>/<Verb> only when the following criteria are met:
- The Message type for both source and destination is request
<Version> (child of<Copy>)
Determines whether the HTTP version is copied from the source request to the destination request. This element has no effect on a response.
Iftrue, copies the HTTP version found in the<Copy> element'ssource attribute to the object specified by the<AssignTo> element.
| Default Value | False |
| Required? | Optional |
| Type | Boolean |
| Parent Element | <Copy> |
| Child Elements | None |
The<Version> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Copy source="VARIABLE_NAME"> <Version>[false|true]</Version> </Copy></AssignMessage>
Example 1
The following example sets<Version> totrue on the request, which copies the version from the default request object to a new, custom request object:
<AssignMessagename="copy-version-1"><Copysource="request"><Version>true</Version></Copy><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
You can use<Copy>/<Version> only when the following criteria are met:
- The Message type for both source and destination is request
<DisplayName>
Use in addition to thename attribute to label the policy in the management UI proxy editor with a different, more natural-sounding name.
The<DisplayName> element is common to all policies.
| Default Value | N/A |
| Required? | Optional. If you omit<DisplayName>, the value of the policy'sname attribute is used. |
| Type | String |
| Parent Element | <PolicyElement> |
| Child Elements | None |
The<DisplayName> element uses the following syntax:
Syntax
<PolicyElement><DisplayName>POLICY_DISPLAY_NAME</DisplayName> ...</PolicyElement>
Example
<PolicyElement><DisplayName>My Validation Policy</DisplayName></PolicyElement>
The<DisplayName> element has no attributes or child elements.
<IgnoreUnresolvedVariables>
Determines whether processing stops when an unresolved variable is encountered.
| Default Value | False |
| Required? | Optional |
| Type | Boolean |
| Parent Element | <AssignMessage> |
| Child Elements | None |
Set totrue to ignore unresolved variables and continue processing; otherwisefalse. The default value isfalse.
Setting<IgnoreUnresolvedVariables> totrue is different from setting the<AssignMessage>'scontinueOnError totrue in that it is specific to setting and getting values of variables. If you setcontinueOnError totrue, then Apigee ignores all errors, not just errors encountered when using variables.
The<IgnoreUnresolvedVariables> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <IgnoreUnresolvedVariables>[true|false]</IgnoreUnresolvedVariables></AssignMessage>
Example 1
The following example sets<IgnoreUnresolvedVariables> totrue:
<AssignMessagename="AM-Set-Headers"><Set><Headers><Headername='new-header'>{possibly-defined-variable}<Header></Headers></Set><IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables></AssignMessage>
Because<IgnoreUnresolvedVariables> is set totrue, if thepossibly-defined-variable variable is not defined, this policy will not throw a fault.
<Remove>
Removes headers, query parameters, form parameters, and/or the message payload from a message. An empty<Remove> tag removes everything from the message.
The affected message can be a request or a response. You specify which message<Remove> acts on by using the<AssignTo> element.
| Default Value | N/A |
| Required? | Optional |
| Type | Complex type |
| Parent Element | <AssignMessage> |
| Child Elements | <FormParams><Headers><Payload><QueryParams> |
A common use case for<Remove> is to delete a query parameter or header that contains sensitive information from the incoming request object, to avoid passing it to the backend server.
The<Remove> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><!--Canalsobeemptytoremoveeverythingfromthemessage(<Remove/>)--><Remove><!--Removeallformparameters--><FormParams/><!--or,removespecificformparametersbyname--><FormParams><FormParamname="FORMPARAM_NAME"/><!--or--><FormParamname="FORMPARAM_NAME">[false|true]</FormParam>...</FormParams><!--Removeallheaders--><Headers/><!--or,removespecificheadersbyname--><Headers><Headername="HEADER_NAME"/><!--or--><Headername="HEADER_NAME">[false|true]</Header>...</Headers><Payload>[false|true]</Payload><!--Removeallqueryparameters--><QueryParams/><!--or,removespecificqueryparametersbyname--><QueryParams><QueryParamname="QUERYPARAM_NAME"/><!--or--><QueryParamname="QUERYPARAM_NAME">[false|true]</QueryParam>...</QueryParams></Remove></AssignMessage>
Example 1
The following example removes the message's body from the response:
<AssignMessagename="AM-remove-1"><DisplayName>remove-1</DisplayName><Remove><Payload>true</Payload></Remove><AssignTo>response</AssignTo></AssignMessage>
In the response flow, this policy removes the body of the response, returning only HTTP headers to the client.
Example 2
The following example removes all form parameters and a query parameter from therequest object:
<AssignMessage name="AM-remove-2"> <Remove> <!-- Empty (<FormParams/>) removes all form parameters --> <FormParams/> <QueryParams> <QueryParam name="qp1"/> </QueryParams> </Remove> <AssignTo>request</AssignTo></AssignMessage>
Example 3
The following example removes everything from a message object:
<AssignMessage name="AM-remove-3"> <Remove/> <AssignTo>request</AssignTo></AssignMessage>
Typically you would do this only if you were going to use the<Set> element or the<Copy> element to set some replacement values into the message.
<FormParams> (child of<Remove>)
Removes the specified form parameters from the request. This element has no effect on a response.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<FormParam> elements or an empty array |
| Parent Element | <Remove> |
| Child Elements | <FormParam> |
The<FormParams> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <!-- Can also be empty to remove everything from the message (<Remove/>) --> <Remove> <!-- Remove all form parameters --> <FormParams/> <!-- or, remove specific form parameters by name --> <FormParams> <FormParam name="FORMPARAM_NAME"/> <!-- or --> <FormParam name="FORMPARAM_NAME">[false|true]</FormParam> ... </FormParams> </Remove></AssignMessage>
Example 1
The following example removes three form parameters from the request:
<AssignMessage name="AM-remove-formparams-1"> <Remove> <FormParams> <FormParam name="form_param_1"/> <FormParam name="form_param_2"/> <FormParam name="form_param_3"/> </FormParams> </Remove> <AssignTo>request</AssignTo></AssignMessage>
Example 2
The following example removes all form parameters from the request:
<AssignMessage name="AM-remove-formparams-2"> <Remove> <FormParams/> </Remove> <AssignTo>request</AssignTo></AssignMessage>
Example 3
If there are multiple form params with the same name, use the following syntax:
<AssignMessage name="AM-remove-formparams-3"> <Remove> <FormParams> <FormParam name="f1"/> <FormParam name="f2"/> <FormParam name="f3.2"/> </FormParams> </Remove> <AssignTo>request</AssignTo></AssignMessage>
This example removesf1,f2, and the second value off3. Iff3 has only one value, then it is not removed.
You can use<FormParams> only when the following criteria are met:
- Message type: Request
Content-Type:application/x-www-form-urlencoded
<Headers> (child of<Remove>)
Removes the specified HTTP headers from the request or response, which is specified by the<AssignTo> element.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<Header> elements or an empty array |
| Parent Element | <Remove> |
| Child Elements | <Header> |
The<Headers> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <!-- Can also be empty to remove everything from the message (<Remove/>) --> <Remove> <!-- Remove all headers --> <Headers/> <!-- or, remove specific headers by name --> <Headers> <Header name="HEADER_NAME"/> <!-- or --> <Header name="HEADER_NAME">[false|true]</Header> ... </Headers> </Remove></AssignMessage>
Example 1
The following example removes theuser-agent header from the request:
<AssignMessage name="AM-remove-one-header"> <Remove> <Headers> <Header name="user-agent"/> </Headers> </Remove> <AssignTo>request</AssignTo></AssignMessage>
Example 2
The following example removes all headers from the request:
<AssignMessage name="AM-remove-all-headers"> <Remove> <Headers/> </Remove> <AssignTo>request</AssignTo></AssignMessage>
Example 3
If there are multiple headers with the same name, use the following syntax:
<AssignMessage name="AM-remove-headers-3"> <Remove> <Headers> <Header name="h1"/> <Header name="h2"/> <Header name="h3.2"/> </Headers> </Remove> <AssignTo>request</AssignTo></AssignMessage>
This example removesh1,h2, and the second value ofh3 from the request. Ifh3 has only one value, then it is not removed.
<Payload> (child of<Remove>)
Determines whether<Remove> deletes the payload in the request or response, which is specified by the<AssignTo> element. Set totrue to clear the payload; otherwisefalse. The default value isfalse.
| Default Value | False |
| Required? | Optional |
| Type | Boolean |
| Parent Element | <Remove> |
| Child Elements | None |
The<Payload> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><!--Canalsobeemptytoremoveeverythingfromthemessage(<Remove/>)--><Remove><Payload>[false|true]</Payload></Remove></AssignMessage>
Example 1
The following example sets<Payload> totrue so that the request payload is cleared:
<AssignMessagename="AM-remove-payload-1"><Remove><Payload>true</Payload></Remove><AssignTo>request</AssignTo></AssignMessage>
<QueryParams> (child of<Remove>)
Removes the specified query parameters from the request. This element has no effect on a response.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<QueryParam> elements or an empty array |
| Parent Element | <Remove> |
| Child Elements | <QueryParam> |
The<QueryParams> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <!-- Can also be empty to remove everything from the message (<Remove/>) --> <Remove> <!-- Remove all query parameters --> <QueryParams/> <!-- or, remove specific query parameters by name --> <QueryParams> <QueryParam name="QUERYPARAM_NAME"/> <!-- or --> <QueryParam name="QUERYPARAM_NAME">[false|true]</QueryParam> ... </QueryParams> </Remove></AssignMessage>
Example 1
The following example removes theapikey query parameter from the request:
<AssignMessage name="AM-remove-query-param"> <Remove> <QueryParams> <QueryParam name="apikey"/> </QueryParams> </Remove> <AssignTo>request</AssignTo></AssignMessage>
Example 2
The following example removes all query parameters from the request:
<AssignMessage name="AM-remove-queryparams-2"> <Remove> <QueryParams/> </Remove> <AssignTo>request</AssignTo></AssignMessage>
Example 3
If there are multiple query params with the same name, use the following syntax:
<AssignMessage name="AM-remove-queryparams-3"> <Remove> <QueryParams> <QueryParam name="qp1"/> <QueryParam name="qp2"/> <QueryParam name="qp3.2"/> </QueryParams> </Remove> <AssignTo>request</AssignTo></AssignMessage>
This example removesqp1,qp2, and the second value ofqp3 from the request. Ifqp3 has only one value, then it is not removed.
You can use<QueryParams> only when the following criteria are met:
- HTTP verbs:
GET,POST,PATCH,DELETE - Message type: Request
<Set>
Sets information in the request or response message, which is specified by the<AssignTo> element.<Set> overwrites headers or query or form parameters that already exist in the original message or adds new if they don't.
Headers and query and form parameters in an HTTP message might hold multiple values. To add additional values for a header or parameter, use the<Add> element instead.
| Default Value | N/A |
| Required? | Optional |
| Type | Complex type |
| Parent Element | <AssignMessage> |
| Child Elements | <Authentication><FormParams><Headers><Payload><Path><QueryParams><StatusCode><Verb><Version> |
<Set> support the dynamic string substitution feature calledmessage templating.The<Set> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Set><Authentication><HeaderName>HEADER_NAME</HeaderName><!--UseeitherGoogleAccessTokenorGoogleIDToken--><GoogleAccessToken><Scopes><Scope>SCOPE</Scope>...</Scopes></GoogleAccessToken>-----or-----<GoogleIDToken><Audienceref='FLOW_VARIABLE_NAME>TARGET_URL</Scope></GoogleAccessToken></Authentication><FormParams><FormParamname="FORMPARAM_NAME">FORMPARAM_VALUE</FormParam>...</FormParams><Headers><Headername="HEADER_NAME">HEADER_VALUE</Header>...</Headers><Path>PATH</Path><PayloadcontentType="CONTENT_TYPE"variablePrefix="PREFIX"variableSuffix="SUFFIX">NEW_PAYLOAD</Payload><QueryParams><QueryParamname="QUERYPARAM_NAME">QUERYPARAM_VALUE</QueryParam>...</QueryParams><StatusCode>HTTP_STATUS_CODEor{variable}</StatusCode><Verb>[GET|POST|PUT|PATCH|DELETE|{variable}]</Verb><Version>[1.0|1.1|{variable}]</Verb></Set></AssignMessage>
Example 1
The following example sets a specific header. When this policy is attached in the Request flow, it will allow the upstream system to receive an additional header that was not included in the original inbound request.
<AssignMessage name="AM-Set-Header"> <Set> <Headers> <Header name="authenticated-developer">{verifyapikey.VAK-1.developer.id}</Header> </Headers> </Set> <AssignTo>request</AssignTo></AssignMessage>Example 2
The following example overwrites the payload for a response, as well as theContent-Type header.
<AssignMessagename="AM-Overwrite-Payload"><Set><PayloadcontentType="application/json">{"status":42}</Payload></Set><AssignTo>response</AssignTo></AssignMessage>
<Authentication> (child of<Set>)
Generates aGoogle OAuth 2.0 access token or Google-issuedOpenID Connect ID token and sets it into theAuthorization header. This is useful when the proxy needs to make authenticated calls to Google services and custom services running on certain Google Cloud products, such asCloud Functions andCloud Run. Use of this element requires setup and deployment steps described inUsing Google authentication. With proper setup, the policy creates a token for you and adds it to the appropriate header in the request.
The child elements,GoogleAccessToken andGoogleIDToken, let you configure the policy to generate either a Google OAuth or OpenID Connect token. You select one of these child elements based on the requirement of the service you wish to call.
| Default Value | N/A |
| Required? | Optional |
| Type | Complex type |
| Parent Element | <Set> |
| Child Elements | <HeaderName><GoogleAccessToken><GoogleIdToken> |
TheAuthentication element uses the following syntax:
Syntax
<AssignMessage> ... <Set> <Authentication> <HeaderName>HEADER_NAME</HeaderName> --EITHER-- <GoogleAccessToken> <Scopes> <Scope>SCOPE</Scope> ... </Scopes> <GoogleAccessToken> --OR-- <GoogleIDToken> <Audience ref="FLOW_VARIABLE">TARGET_URL</Audience> </GoogleIDToken> </Authentication> </Set> ...</AssignMessage>
Using Access Token
The following example shows theGoogleAccessToken element:
<Authentication> <GoogleAccessToken> <Scopes> <Scope>https://www.googleapis.com/auth/cloud-platform</Scope> </Scopes> </GoogleAccessToken></Authentication>
Using ID Token
The following example shows theGoogleIDToken element:
<Authentication> <GoogleIDToken> <Audience>https://httpserver0-bar.run.app</Audience> </GoogleIDToken></Authentication>
Using HeaderName
The following example shows theHeaderName element:
<Authentication> <HeaderName>Authorization</HeaderName> <GoogleAccessToken> <Scopes> <Scope>https://www.googleapis.com/auth/cloud-platform</Scopes> </Scopes> </GoogleAccessToken> </Authentication>
<HeaderName> (child of<Authentication>)
By default, when an Authentication configuration is present, Apigee generates a bearer token and injects it into theAuthorization header in the message sent to the target system. TheHeaderName element allows you to specify the name of a different header to hold that bearer token. TheAuthorization header, if present, is left unmodified and also sent in the request.
| Default Value | N/A |
| Required? | Optional |
| Type | String |
| Parent Element | <Authentication> |
| Child Elements | None |
TheHeaderName element uses the following syntax:
Syntax
<AssignMessage>... <Authentication> <HeaderName>HEADER_NAME</HeaderName> <GoogleAccessToken> ... </GoogleAccessToken> </Authentication> ...</AssignMessage>
With static string
In this example, the generated bearer token is added, by default, to a header namedAuthorization that is sent to the target system. TheAuthorization header, if present, is left unmodified and also sent in the request.
<Authentication> <HeaderName>Authorization</HeaderName> <GoogleAccessToken> <Scopes> <Scope>https://www.googleapis.com/auth/cloud-platform</Scope> </Scopes> </GoogleAccessToken></Authentication>
With variable reference
In this example, the generated bearer token is added, by default, to a header namedAuthorization that is sent to the target system. Ifmy-variable has a value, that value is used instead of the default string. TheAuthorization header, if present, is left unmodified and also sent in the request.
<Authentication><HeaderNameref='my-variable'>Authorization</HeaderName><GoogleAccessToken><Scopes><Scopes>https://www.googleapis.com/auth/cloud-platform</Scopes></Scopes>>/GoogleAccessToken></Authentication>
<GoogleAccessToken> (child of<Authentication>)
GeneratesGoogle OAuth 2.0 tokensto make authenticated calls to Google services. Google OAuth tokens can be used to call many kinds of Google services, such asCloud Logging andSecret Manager.
| Default Value | N/A |
| Required? | Either theGoogleAccessToken orGoogleIDToken child element must be present. |
| Type | String |
| Parent Element | <Authentication> |
| Child Elements | <Scopes> |
TheGoogleAccessToken element uses the following syntax:
Syntax
<AssignMessage>... <Authentication> <GoogleAccessToken> <Scopes> <Scope>SCOPE_1</Scope> ... </Scopes> >/GoogleAccessToken> </Authentication> ...</AssignMessage>
Example 1
The following example shows theGoogleAccessToken element:
<Authentication> <GoogleAccessToken> <Scopes> <Scope>https://www.googleapis.com/auth/cloud-platform</Scopes> </Scopes> </GoogleAccessToken></Authentication>
<Scopes> (child of<GoogleAccessToken>)
Identifies the scopes to be included in the OAuth 2.0 access token. For more information, see OAuth 2.0 Scopes for Google APIs. You can add one or more<Scope> child elements under this element.
| Default Value | N/A |
| Required? | Required |
| Type | String |
| Parent Element | <GoogleAccessToken> |
| Child Elements | <Scope> |
TheScopes element uses the following syntax:
<Scopes> <Scope>SCOPE_1</Scope> <Scope>SCOPE_2</Scope> ... </Scopes>
<Scope> (child of<Scopes>)
Specifies a valid Google API scope. For more information, see OAuth 2.0 Scopes for Google APIs.
| Default Value | N/A |
| Required? | At least one value is required. |
| Type | String |
| Parent Element | <Scopes> |
| Child Elements | None |
TheScope element uses the following syntax:
<Scope>SCOPE_1</Scope>
<GoogleIDToken> (child of<GoogleAccessToken>)
Generates Google-issuedOpenID Connect tokens to make authenticated calls to Google services.
| Default Value | N/A |
| Required? | Either theGoogleAccessToken orGoogleIDToken child element must be present. |
| Type | String |
| Parent Element | <Authentication> |
| Child Elements | <Audience> |
TheGoogleIDToken element uses the following syntax:
Syntax
<AssignMessage>... <Authentication> <GoogleIDToken> <Audience ref="FLOW_VARIABLE_NAME">TARGET_URL</Audience> </GoogleIDToken> </Authentication></AssignMessage>
Example 1
The following example shows theGoogleIDToken element:
<Authentication> <GoogleIDToken> <Audience>https://httpserver0-bar.run.app</Audience> </GoogleIDToken></Authentication>
<Audience> (child of<GoogleAccessToken>)
The audience for the generated authentication token, such as the API or account that the token grants access to.
If the value ofAudience is empty or theref variable does not resolve to a valid value, anduseTargetUrl istrue, then the URL of the target (excluding any query parameters) is used as the audience. By default,useTargetUrl isfalse.
| Default Value | N/A |
| Required? | Required |
| Type | String |
| Parent Element | |
| Child Elements | None |
TheAudience element uses the following syntax:
<Audience>TARGET_URL</Audience>or: <Audience ref='FLOW_VARIABLE_NAME'/>
<FormParams> (child of<Set>)
Overwrites existing form parameters on a request and replaces them with the new values that you specify with this element. This element has no effect on a response.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<FormParam> elements |
| Parent Element | <Set> |
| Child Elements | <FormParam> |
The<FormParams> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Set> <FormParams> <FormParam name="FORMPARAM_NAME">FORMPARAM_VALUE</FormParam> ... </FormParams> </Set></AssignMessage>
Example 1
The following example sets a form parameter calledmyparam to the value of therequest.header.myparam variable in a new, custom request:
<AssignMessagename="AM-set-formparams-1"><Set><FormParams><FormParamname="myparam">{request.header.myparam}</FormParam></FormParams></Set><AssignTocreateNew="true"transport="http"type="request">MyCustomRequest</AssignTo></AssignMessage>
You can use<FormParams> only when the following criteria are met:
- HTTP verb:
POST - Message type: Request
If you set empty form parameters in your policy (<Set><FormParams/></Set>), the policy does not add any form parameters. This is the same as omitting the<FormParams>.
When you use<Set> with<FormParams>, Apigee changes theContent-Type of the message toapplication/x-www-form-urlencoded.
<Headers> (child of<Set>)
Overwrites existing HTTP headers in the request or response, which is specified by the<AssignTo> element.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<Header> elements |
| Parent Element | <Set> |
| Child Elements | <Header> |
The<Headers> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Set> <Headers> <Header name="HEADER_NAME">HEADER_VALUE</Header> ... </Headers> </Set></AssignMessage>
Example 1
The following example sets thex-ratelimit-remaining header to the value of theratelimit.Quota-1.available.count variable:
<AssignMessage name="AM-Set-RateLimit-Header"> <Set> <Headers> <Header name="X-RateLimit-Remaining">{ratelimit.Quota-1.available.count}</Header> </Headers> </Set> <AssignTo>response</AssignTo></AssignMessage>If you define empty headers in your policy (<Set><Headers/></Set>), the policy does not set any headers. This will have the same effect as omitting<Headers>.
<Path> (child of<Set>)
Note:Sometimes you might need to send a message to a URL other than the URL defined in an API proxy'sTargetEndpoint. If you want to rewrite the target URL, override thetarget.url variable by configuring an AssignMessage policy with the<AssignVariable> element, and attaching it to theTargetEndpoint. (You can't overridetarget.url in a policy attached to theProxyEndpoint, because Apigee initializes the variable in theTargetEndpoint).
The following example shows how a policy could overridetarget.url. With this policy attached to theTargetEndpoint request, the proxy would call the URL below instead of the URL defined on theTargetEndpoint.
<AssignMessage name="AM-Override-Target-URL"> ... <AssignVariable> <Name>target.url</Name> <Value>https://mocktarget.apigee.net/user?user=Dude</Value> </AssignVariable> ...
You can also use aJavaScript policy in theTargetEndpoint to override thetarget.url.
<Payload> (child of<Set>)
Defines the message body for a request or response, which is specified by the<AssignTo> element. The payload can be any valid content type, such as plain text, JSON, or XML.
| Default Value | empty string |
| Required? | Optional |
| Type | String |
| Parent Element | <Set> |
| Child Elements | None |
The<Payload> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Set><PayloadcontentType="CONTENT_TYPE"variablePrefix="PREFIX"variableSuffix="SUFFIX">NEW_PAYLOAD</Payload></Set></AssignMessage>
Example 1
The following example sets a plain text payload:
<AssignMessagename="AM-set-payload-1"><Set><PayloadcontentType="text/plain">42</Payload></Set></AssignMessage>
Example 2
The following example sets a JSON payload:
<AssignMessagename="AM-set-payload-2"><Set><PayloadcontentType="application/json">{"name":"foo","type":"bar"}</Payload></Set></AssignMessage>
Example 3
The following example inserts variable values into the payload by wrapping variable names in curly braces:
<AssignMessagename="AM-set-payload-3"><Set><PayloadcontentType="application/json">{"name":"foo","type":"{variable_name}"}</Payload></Set></AssignMessage>
In older versions of Apigee—for example, prior to cloud release 16.08.17—you could not use curly braces to denote variable references within JSON payloads. In those releases, you needed to use thevariablePrefix andvariableSuffix attributes to specify delimiter characters, and use those to wrap variable names, like so:
<AssignMessagename="AM-set-payload-3b"><Set><PayloadcontentType="application/json"variablePrefix="@"variableSuffix="#">{"name":"foo","type":"@variable_name#"}</Payload></Set></AssignMessage>
This older syntax still works.
Example 4
The content of<Payload> is treated as a message template. This means that the AssignMessage policy replaces variables wrapped in curly braces with the value of the referenced variables at runtime.
The following example uses the curly braces syntax to set part of the payload to a variable value:
<AssignMessagename="AM-set-payload-4"><Set><PayloadcontentType="text/xml"><root><e1>sunday</e1><e2>funday</e2><e3>{var1}</e3></root></Payload></Set></AssignMessage>
The following table describes the attributes of<Payload>:
| Attribute | Description | Presence | Type |
|---|---|---|---|
contentType | If specified, the value of | Optional | String |
variablePrefix | Optionally specifies the leading delimiter on a flow variable. Defaults to "{". For more information, seeFlow variables reference. | Optional | Char |
variableSuffix | Optionally specifies the trailing delimiter on a flow variable. Defaults to "}". For more information, seeFlow variables reference. | Optional | Char |
<QueryParams> (child of<Set>)
Overwrites existing query parameters in the request with new values. This element has no effect on a response.
| Default Value | N/A |
| Required? | Optional |
| Type | Array of<QueryParam> elements |
| Parent Element | <Set> |
| Child Elements | <QueryParam> |
The<QueryParams> element uses the following syntax:
Syntax
<AssignMessage continueOnError="[false|true]" enabled="[true|false]" name="POLICY_NAME" > <Set> <QueryParams> <QueryParam name="QUERYPARAM_NAME">QUERYPARAM_VALUE</QueryParam> ... </QueryParams> </Set></AssignMessage>
Example 1
The following example sets theaddress query parameter to the value of therequest.header.address variable:
<AssignMessagename="AM-set-queryparams-1"><Set><QueryParams><QueryParamname="address">{request.header.address}</QueryParam></QueryParams></Set></AssignMessage>
You can use<Set>/<QueryParams> only when the following criteria are met:
- HTTP verbs:
GET,PUT,POST,PATCH,DELETE - Message type: Request
If you define empty query parameters in your policy (<Set><QueryParams/></Set>), the policy does not set any query parameters. This is the same as omitting<QueryParams>.
<StatusCode> (child of<Set>)
Sets the status code on the response. This element has no effect on a request.
| Default Value | '200' (when<AssignTo>'screateNew attribute is set to 'true') |
| Required? | Optional |
| Type | String orVARIABLE |
| Parent Element | <Set> |
| Child Elements | None |
The<StatusCode> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Set><StatusCode>HTTP_STATUS_CODEor{variable}</StatusCode></Set></AssignMessage>
Example 1
The following example sets a simple status code:
<AssignMessage name="AM-set-statuscode-404"> <Set> <StatusCode>404</StatusCode> </Set> <AssignTo>response</AssignTo></AssignMessage>
Example 2
The content of<StatusCode> is treated as a message template. This means a variable name wrapped in curly braces will be replaced at runtime with the value of the referenced variable, as the following example shows:
<AssignMessage name="set-statuscode-2"> <Set> <StatusCode>{calloutresponse.status.code}</StatusCode> </Set> <AssignTo>response</AssignTo></AssignMessage>You can use<Set>/<StatusCode> only when the following criteria are met:
- Message type: Response
<Verb> (child of<Set>)
Sets the HTTP verb on the request. This element has no effect on a response.
| Default Value | N/A |
| Required? | Optional |
| Type | String orVARIABLE |
| Parent Element | <Set> |
| Child Elements | None |
The<Verb> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Set><Verb>[GET|POST|PUT|PATCH|DELETE|{variable}]</Verb></Set></AssignMessage>
Example 1
The following example sets a simple verb on the request:
<AssignMessage name="AM-set-verb-1"> <Set> <Verb>POST</Verb> </Set> <AssignTo>request</AssignTo></AssignMessage>
Example 2
The content of<Verb> is treated as a message template. This means a variable name wrapped in curly braces will be replaced at runtime with the value of the referenced variable.
The following example uses a variable to populate a verb:
<AssignMessagename="AM-set-verb-to-dynamic-value"><Set><Verb>{my_variable}</Verb></Set><AssignTo>request</AssignTo></AssignMessage>
You can use<Set>/<Verb> only when the following criteria are met:
- Message type: Request
<Version> (child of<Set>)
Sets the HTTP version on a request. This element has no effect on a response.
| Default Value | N/A |
| Required? | Optional |
| Type | String orVARIABLE |
| Parent Element | <Set> |
| Child Elements | None |
The<Version> element uses the following syntax:
Syntax
<AssignMessagecontinueOnError="[false|true]"enabled="[true|false]"name="POLICY_NAME"><Set><Version>[1.0|1.1|{variable}]</Verb></Set></AssignMessage>
Example 1
The following example sets the version number to1.1:
<AssignMessage name="AM-set-version-1"> <Set> <Version>1.1</Version> </Set> </AssignMessage>
Example 2
The following uses a variable in curly braces to set the version number:
<AssignMessage name="AM-set-version-2"> <Set> <Version>{my_version}</Version> </Set> <AssignTo>request</AssignTo></AssignMessage>The content of<Version> is treated as a message template. This means a variable name wrapped in curly braces will be replaced at runtime with the value of the referenced variable.
You can use<Set>/<Version> only when the following criteria are met:
- Message type: Request
Create custom request messages
You can use AssignMessage to create a custom request message. After you create a custom request, you can use it in the following ways:
- Access its variables in other policies
- Pass it to an external service
To create a custom request message, use the<AssignTo> element in your AssignMessage policy. SetcreateNew totrue and specify the name of the new message in the body of the element, as the following example shows:
<AssignMessagename="assignto-2"><AssignTocreateNew="true"transport="http"type="request">MyRequestObject</AssignTo>...</AssignMessage>
By default, Apigee does nothing with the custom request message. After creating it, Apigee will continue through the flow with the original request. To use the custom request, add a policy such as theServiceCallout policy to your proxy, and explicitly reference the newly created request message in the configuration for that policy. This would allow you to pass the custom request to an external service endpoint.
The following examples create custom request messages:
Example 1
The following example creates a custom request object with AssignMessage:
<AssignMessagename="AssignMessage-3"><AssignTocreateNew="true"type="request">MyCustomRequest</AssignTo><Copy><Headers><Headername="user-agent"/></Headers></Copy><Set><QueryParams><QueryParamname="address">{request.queryparam.addy}</QueryParam></QueryParams><Verb>GET</Verb></Set><IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables></AssignMessage>
This example:
- Creates a new request message object called
MyCustomRequest. - On MyCustomRequest, this policy:
- Copies the value of the
user-agentHTTP header from the incoming request to the new message. Because<Copy>does not specify thesourceattribute, Apigee will use therequestmessage as the source to copyfrom. - Sets the
addressquery parameter on the custom message to the value of the incoming request'saddyquery parameter. - Sets the HTTP verb to
GET.
- Copies the value of the
- Sets
<IgnoreUnresolvedVariables>tofalse. When<IgnoreUnresolvedVariables>isfalse, if one of the variables referenced in the policy configuration does not exist, Apigee will enterfault state in the API flow.
Example 2
Here's another example demonstrating how to create a custom request object with AssignMessage:
<AssignMessagename="AssignMessage-2"><AssignTocreateNew="true"type="request">partner.request</AssignTo><Set><Verb>POST</Verb><PayloadcontentType="text/xml"><request><operation>105</operation></request></Payload></Set></AssignMessage>
This example creates a new custom request calledpartner.request. It then sets the<Verb> and<Payload> on the new request.
You can access the various properties of a custom message in another AssignMessage policy that occurs later in the flow. The following example gets the value of a header from a named custom response, and places it into a new header in the request message:
<AssignMessage name="AM-Copy-Custom-Header"> <AssignTo>request</AssignTo> <Set> <Headers> <Header name="injected-approval-id">{MyCalloutResponse.header.approval-id}</Header> </Headers> </Set></AssignMessage>Error codes
This section describes the fault codes and error messages that are returned and fault variables that are set by Apigee when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, seeWhat you need to know about policy errors andHandling faults.
Runtime errors
These errors can occur when the policy executes.
Tip: Need help resolving an error? Clickbuild in theFix column for detailed troubleshooting information.| Fault code | HTTP status | Cause | Fix |
|---|---|---|---|
steps.assignmessage.SetVariableFailed | 500 | The policy was not able to set a variable. See the fault string for the name of the unresolved variable. | |
steps.assignmessage.VariableOfNonMsgType | 500 | This error occurs if the Message type variables represent entire HTTP requests and responses. The built-in Apigee flow variables | build |
steps.assignmessage.UnresolvedVariable | 500 | This error occurs if a variable specified in the AssignMessage policy is either:
| build |
Deployment errors
These errors can occur when you deploy a proxy containing this policy.
Tip: Need help resolving an error? Clickbuild next to the error's cause description to visit the troubleshooting page.| Error name | Cause | Fix |
|---|---|---|
InvalidIndex | If the index specified in the<Copy> and/or<Remove> elements of theAssignMessage policy is0 or a negative number, then deployment of the API Proxy fails. | build |
InvalidVariableName | If the child element<Name> is empty or not specified in the<AssignVariable> element, then the deployment of the API proxy fails because there is no valid variable name to which to assign a value. A valid variable name is required. | build |
InvalidPayload | A payload specified in the policy is invalid. |
Fault variables
These variables are set when this policy triggers an error at runtime. For more information, seeWhat you need to know about policy errors.
| Variables | Where | Example |
|---|---|---|
fault.name="FAULT_NAME" | FAULT_NAME is the name of the fault, as listed in theRuntime errors table above. The fault name is the last part of the fault code. | fault.name Matches "UnresolvedVariable" |
assignmessage.POLICY_NAME.failed | POLICY_NAME is the user-specified name of the policy that threw the fault. | assignmessage.AM-SetResponse.failed = true |
Example error response
Note: For error handling, the best practice is to trap theerrorcode part of the error response. Do not rely on the text in thefaultstring, because it could change.{"fault":{"detail":{"errorcode":"steps.assignmessage.VariableOfNonMsgType"},"faultstring":"AssignMessage[AM-SetResponse]: value of variable is not of type Message"}}
Example fault rule
<FaultRulename="Assign Message Faults"><Step><Name>AM-CustomNonMessageTypeErrorResponse</Name><Condition>(fault.nameMatches"VariableOfNonMsgType")</Condition></Step><Step><Name>AM-CustomSetVariableErrorResponse</Name><Condition>(fault.name="SetVariableFailed")</Condition></Step><Condition>(assignmessage.failed=true)</Condition></FaultRule>
Schemas
Each policy type is defined by an XML schema (.xsd). For reference,policy schemas are available on GitHub.
Related topics
Working samples of the AssignMessage policy are available in theAPI Platform samples.
For a more advanced example of how to override thetarget.url from theProxyEndpoint, seethis Apigee Community article.
To see aset path in action in aServiceCallout policy, check out thisLearn by doing example in the Apigee GitHub samples. Just clone the repository and follow the instructions in that topic. The example uses AssignMessage to set a request path, then uses aServiceCallout policy to make the request to an external service.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-17 UTC.