The following example uses three Assign Message policies:

  1. Creates three flow variables in the request, with static values
  2. Gets the flow variables dynamically in a second policy in the request flow
  3. Sets them in the payload of the response
<!-- Policy #1: Set variables in the request -->
<AssignMessage continueOnError="false" enabled="true" name="set-variables">
    <!-- Create a variable named myAppSecret -->
    <AssignVariable>
        <Name>myAppSecret</Name>
        <Value>42</Value>
    </AssignVariable>
    <!-- Create a variable named config.environment -->
    <AssignVariable>
        <Name>config.environment</Name>
        <Value>test</Value>
    </AssignVariable>
    <!-- Create a variable named config.protocol -->
    <AssignVariable>
        <Name>config.protocol</Name>
        <Value>gopher</Value>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</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 -->
<AssignMessage continueOnError="false" enabled="true" name="get-variables">
  <AssignTo createNew="false" transport="http" type="request"/>
  <!-- Get the value of myAppSecret and create a new variable, secret -->
  <AssignVariable>
    <Name>secret</Name>
    <Ref>myAppSecret</Ref>
    <Value>0</Value>
  </AssignVariable>
  <!-- Get the value of config.environment and create a new variable, environment -->
  <AssignVariable>
    <Name>environment</Name>
    <Ref>config.environment</Ref>
    <Value>default</Value>
  </AssignVariable>
  <!-- Get the value of config.protocol and create a new variable, 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:

  1. Add policies #1 and #2 to the request flow. Be sure to put policy #1 before policy #2.
  2. Add the third policy in the response flow.
  3. 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 -->
    <AssignMessage continueOnError="false" enabled="true" name="put-em-in-the-payload">
      <DisplayName>put-em-in-the-payload</DisplayName>
      <Set>
        <Payload contentType="application/xml">
          <wrapper>
            <secret>{secret}</secret>
            <config>
              <environment>{environment}</environment>
              <protocol>{protocol}</protocol>
            </config>
          </wrapper>
        </Payload>
      </Set>
      <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
      <AssignTo createNew="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's contentType attribute to "application/xml".

  4. 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 xmllint so 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:

    <wrapper>
      <secret>42</secret>
      <config>
        <environment>test</environment>
        <protocol>gopher</protocol>
      </config>
    </wrapper>