以下示例使用了三个“分配消息”政策:

  1. 使用静态值在请求中创建三个流变量
  2. 动态获取请求流的第二个政策中的流变量
  3. 在响应的载荷中设置这些变量
<!-- 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>

在第一个政策中,<AssignVariable> 元素在请求中创建并设置三个变量。每个 <Name> 元素都指定一个变量名称,<Value> 指定值。

第二个政策使用 <AssignVariable> 元素读取值并创建三个新的变量:

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

在第二个政策中,<Ref> 元素引用来源变量,<Name> 元素指定新变量的名称。如果 <Ref> 元素引用的变量无法访问,则可以使用由 <Value> 元素指定的值。

如需试用这组政策,请执行以下操作

  1. 将政策 1 和政策 2 添加到请求流。请务必将政策 1 放置在政策 2 前面
  2. 在响应流中添加第三个政策。
  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

    (可选)您可以通过 xmllint 等实用程序传输结果,使得 XML 以格式设置良好的结构显示:

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

    响应的正文应如下所示:

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