以下範例使用三項「指派訊息」政策:
<!-- 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> 元素會在要求中建立及設定三個變數。每個 <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> 元素指定的值。
如要試用這組政策:
<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」。
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>