Và dụ sau đây sá» dụng 3 chÃnh sách Gán thông báo:
<!-- 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 ba biến trong yêu cầu. Mỗi phần tỠ<Name>
chỉ định 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 ba 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à các phần tỠ<Name>
chỉ định tên cá»§a các biến má»›i. Nếu không truy cáºp được biến được tham chiếu bởi phần tá» <Ref>
, 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 ứng dụng 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 các biến flow trong <Set>
là gói các biến đó trong dấu ngoặc nhá»n.
Hãy 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
Nếu muốn, bạn có thể chuyển kết quả qua má»™t tiện Ãch như xmllint
để XML hiển thị theo 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 của phản hồi sẽ có dạng như sau:
<wrapper> <secret>42</secret> <config> <environment>test</environment> <protocol>gopher</protocol> </config> </wrapper>