Hi, I have a requirement to access the variables available in session in Payload of ServiceCallout Policy. But I didn't find any examples/documents related to that. Please share the docs or examples to use the access variables in Payload.
Example: Suppose If I wanted to pass the 'apiproxy.name' in the subject variable in JSON Payload of ServiceCallout. What is the syntax to use it?
Ex ServiceCallout:
<ServiceCallout async="false" continueOnError="false" enabled="true" name="SendMailServiceCallout"> <Request clearPayload="false" variable="myRequest"> <Set> <Headers> <Header name="Accept">application/json</Header> </Headers> <Verb>POST</Verb> <Payload contentType="application/json" variablePrefix="%" variableSuffix="#"> {"from":"test","to":"test1","subject":"Test Subject email","html":"Test body email"} </Payload> </Set> </Request> <Response>calloutResponse</Response> <Timeout>60000</Timeout> <HTTPTargetConnection> <URL>http:/test.apigee.net/v1/abc</URL> <SSLInfo /> <Properties /> </HTTPTargetConnection> </ServiceCallout>
Solved! Go to Solution.
Hi @Swapna
Following change should help in this scenario: "subject":"%apirpoxy.name#"
As @sgilson has suggested, AssignMessagePolicy provides much more flexibility. In fact, ServiceCallout being a generic scenario, recommendation is to use three policy in the sequence; AssignMessage,ServiceCallout, and ExtractVariables. Following resources may be helpful:
1. http://apigee.com/docs/api-services/cookbook/using-policy-composition
Sample code is available here:
https://github.com/apigee/api-platform-samples/tree/master/doc-samples/policy-mashup-cookbook
Cheers, Rajesh Doda
Swapna, have you looked at the AssignMessage policy here:
http://apigee.com/docs/api-services/reference/assign-message-policy
Stephen
Hi @Swapna
Following change should help in this scenario: "subject":"%apirpoxy.name#"
As @sgilson has suggested, AssignMessagePolicy provides much more flexibility. In fact, ServiceCallout being a generic scenario, recommendation is to use three policy in the sequence; AssignMessage,ServiceCallout, and ExtractVariables. Following resources may be helpful:
1. http://apigee.com/docs/api-services/cookbook/using-policy-composition
Sample code is available here:
https://github.com/apigee/api-platform-samples/tree/master/doc-samples/policy-mashup-cookbook
Cheers, Rajesh Doda
Thanks @rdoda I have used the same syntax to get the variables available from session. But it was displayed as string. Mostly all the variables are available in session, so is there anyway that I can access them directly in service callout policy?
Used:
Payload contentType="application/json" variablePrefix="%" variableSuffix="#"> {"from":"abc@gmail.com","to":"xyz@gmail.com","subject":"Test Subject email","html":"@apiproxy.name#"} </Payload>
Output:
I got it now. We have use the "variablePrefix" and "variableSuffix" properties of the payload and use these two properties to get the value.
Saw it in the below link.
https://community.apigee.com/articles/2458/how-do-i-easily-insert-variables-into-json-payload.html
Thanks!
@sgilson Thanks for your quick response.
How I can use those assign variables in the payload of ServiceCallout? The below syntax is not working to get variable "ProxyName" to pass it in the JSON Payload. Please advice.
Example: AssignMessage
<AssignMessage async="false" continueOnError="false" enabled="true" name="AssignVariablesToSendMail"> <DisplayName>AssignVariablesToSendMail</DisplayName> <AssignVariable>
<Name>ProxyName</Name>
<Ref>apiproxy.name</Ref>
</AssignVariable>
</AssignMessage>
Example: ServiceCallout
<Payload contentType="application/json" variablePrefix="%" variableSuffix="#">
{"Name": ProxyName}
</Payload>
You should look at the example here:
http://apigee.com/docs/api-services/cookbook/using-policy-composition
It has a complete example of adding a variable to a request to ServiceCallout.
You seem to be mixing the two ways to pass a variable to the Service Callout. Either create a new request object in the AssignMessage policy containing the variable and pass that to Service Callout, or use the Payload option in Service Callout.
@sgilson. Thanks it's working now.
Good to know that you are able to resolve with this post.
@Swapna I am glad that your issue is resolved. could you please accept the answer that helped you? It helps others in the community as well. Thanks!
User | Count |
---|---|
3 | |
2 | |
1 | |
1 | |
1 |