How do I access variables in a Json Payload that is stored in a context variable?

Hi,

I have a Proxy which does a service call out to a get_oAuthToken service of the backend and gets a JSON Payload and stores it in a context variable "calloutResponse". how do i access the token variable in the JSON payload stored in the context variable. I have the below setup in my assignmessage policy.

<AssignMessage continueOnError="false" enabled="true" name="generateCustomResponse">
  <DisplayName>generateCustomResponse</DisplayName>
  <Set>
    <Payload variableSuffix="@" contentType="application/json" variablePrefix="#">
    { "toke" : #calloutResponse.access_token@ }</Payload>
    <Path/>
  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>
Solved Solved
0 3 487
1 ACCEPTED SOLUTION

Try an ExtractVariables policy before the assign message policy and then use the variables created in the assign message policy.
https://cloud.google.com/apigee/docs/api-platform/reference/policies/extract-variables-policy#json

View solution in original post

3 REPLIES 3

Try an ExtractVariables policy before the assign message policy and then use the variables created in the assign message policy.
https://cloud.google.com/apigee/docs/api-platform/reference/policies/extract-variables-policy#json

This post gives an example on how to use it which really helped.

extract variables from a service-callout 

Preceding your AssignMessage with an ExtractVariables will work.  Alternatively, You can do it all within AssignMessage if you like:

 

<AssignMessage name="AM-generateCustomResponse">
  <AssignVariable>
    <Name>path1</Name>
    <Value>$.access_token</Value>
  </AssignVariable>
  <Set>
    <Payload contentType="application/json">{
  "token" : "{jsonPath(path1,calloutResponse.content)}"
}</Payload>
    <Path/>
  </Set>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</AssignMessage>