다음 예시에서는 세 가지 Assign Message 정책을 사용합니다.
<!-- 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> 요소를 사용하여 값을 읽고 새 변수 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>
두 번째 정책에서 <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
필요한 경우 XML이 올바른 형식의 구조로 표시되도록 xmllint와 같은 유틸리티를 통해 결과를 파이핑할 수 있습니다.
curl -vL https://ahamilton-eval-test.apigee.net/myproxy | xmllint --format -
응답 본문은 다음과 같이 표시됩니다.
<wrapper>
<secret>42</secret>
<config>
<environment>test</environment>
<protocol>gopher</protocol>
</config>
</wrapper>