次の例では、3 つの Assign Message ポリシーを使用しています。

  1. リクエストに 3 つのフロー変数を静的な値で作成します。
  2. リクエスト フローの 2 番目のポリシーでフロー変数を動的に取得します。
  3. レスポンスのペイロードでこれらの変数を設定します。
<!-- Policy #1: Set variables in the request -->

<AssignMessage name="AM-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>
</AssignMessage>

最初のポリシーでは、<AssignVariable> 要素がリクエストに 3 つの変数を作成して設定します。各 <Name> 要素は変数名を指定し、<Value> で値が指定されます。

2 番目のポリシーで <AssignVariable> 要素を使用して値を読み込み、3 つの新しい変数を作成します。

<!-- 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>

2 番目のポリシーでは、<Ref> 要素は参照元の変数を参照し、<Name> 要素は新しい変数の名前を指定します。<Ref> 要素で参照される変数にアクセスできない場合は、<Value> 要素で指定された値を使用できます。

この一連のポリシーを試す

  1. リクエスト フローにポリシー #1 と #2 を追加します。ポリシー #2 の前に、ポリシー #1 を配置してください。
  2. 3 番目のポリシーをレスポンス フローに追加します。
  3. 3 番目のポリシーは、<Set> 要素を使用して変数をレスポンス フローに追加します。次の例では、Edge がクライアントに返すレスポンスに XML ペイロードを作成しています。
    <!-- 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>

    <Set> でフロー変数にアクセスする構文は中かっこで囲む必要があるので注意してください。

    <Payload> 要素の contentType 属性を必ず「application/xml」に設定してください。

  4. 次の例のように、API プロキシにリクエストを送信します。
    curl -vL https://ahamilton-eval-test.apigee.net/myproxy

    必要に応じて、XML が見やすく書式設定された構造で表示されるように、xmllint などのユーティリティを使用して結果をパイプ処理できます。

    curl -vL https://ahamilton-eval-test.apigee.net/myproxy | xmllint --format -

    レスポンスの本文は、次のようになります。

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