Ví dụ sau đây sử dụng 3 chính sách Assign Message:

  1. Tạo 3 biến luồng trong yêu cầu, với các giá trị tĩnh
  2. Lấy các biến luồng một cách linh động trong chính sách thứ hai trong luồng yêu cầu
  3. Đặt các giá trị này trong tải trọng của phản hồi
<!-- 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>

Trong chính sách đầu tiên, phần tử <AssignVariable> sẽ tạo và đặt 3 biến trong yêu cầu. Mỗi phần tử <Name> chỉ định một tên biến và <Value> chỉ định giá trị.

Chính sách thứ hai sử dụng phần tử <AssignVariable> để đọc các giá trị và tạo 3 biến mới:

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

Trong chính sách thứ hai, phần tử <Ref> tham chiếu đến biến nguồn và phần tử <Name> chỉ định tên của các biến mới. Nếu không truy cập được vào biến mà phần tử <Ref> tham chiếu, bạn có thể sử dụng giá trị do phần tử <Value> chỉ định.

Cách dùng thử bộ chính sách này:

  1. Thêm chính sách số 1 và số 2 vào quy trình yêu cầu. Hãy nhớ đặt chính sách số 1 trước chính sách số 2.
  2. Thêm chính sách thứ ba vào quy trình phản hồi.
  3. Chính sách thứ ba sử dụng phần tử <Set> để thêm các biến vào phản hồi. Ví dụ sau đây tạo một tải trọng XML trong phản hồi mà Edge trả về cho máy khách:
    <!-- 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>

    Xin lưu ý rằng cú pháp để truy cập vào các biến luồng trong <Set> là đặt các biến đó trong dấu ngoặc nhọn.

    Nhớ đặt thuộc tính contentType của phần tử <Payload> thành "application/xml".

  4. Gửi yêu cầu đến proxy API của bạn; ví dụ:
    curl -vL https://ahamilton-eval-test.apigee.net/myproxy

    Bạn có thể chuyển kết quả qua một tiện ích như xmllint để XML xuất hiện trong một cấu trúc được định dạng đẹp mắt:

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

    Nội dung phản hồi sẽ có dạng như sau:

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