ตัวอย่างต่อไปนี้ใช้นโยบาย Assign Message 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>
จะสร้างและตั้งค่าตัวแปร 3 รายการในคําขอ องค์ประกอบ <Name>
แต่ละรายการจะระบุชื่อตัวแปร และ <Value>
จะระบุค่า
นโยบายที่ 2 ใช้องค์ประกอบ <AssignVariable>
เพื่ออ่านค่าและสร้างตัวแปรใหม่ 3 รายการ ดังนี้
<!-- 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>
ในนโยบายที่ 2 องค์ประกอบ <Ref>
จะอ้างอิงตัวแปรต้นทาง และองค์ประกอบ <Name>
จะระบุชื่อของตัวแปรใหม่ หากเข้าถึงตัวแปรที่อ้างอิงโดยองค์ประกอบ <Ref>
ไม่ได้ คุณจะใช้ค่าที่ระบุโดยองค์ประกอบ <Value>
ได้
วิธีลองใช้ชุดนโยบายนี้
<Set>
เพื่อเพิ่มตัวแปรลงในคำตอบ ตัวอย่างต่อไปนี้สร้างเพย์โหลด XML ในการตอบกลับที่ Edge ส่งกลับไปยังไคลเอ็นต์
<!-- 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>
คือการห่อตัวแปรเหล่านั้นในวงเล็บปีกกา
อย่าลืมตั้งค่าแอตทริบิวต์ contentType
ขององค์ประกอบ <Payload>
เป็น "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>