Ví dụ sau đây sử dụng 3 chính sách Assign Message:
<!-- 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:
<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".
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>