How can I access the response variable set by the service callout policy from inside of javascript?

Not applicable
 
Solved Solved
2 6 2,593
1 ACCEPTED SOLUTION

Suppose you have a ServiceCallout policy like this:

<ServiceCallout name='ServiceCallout-1'>
  <Request variable='my.custom.request' clearPayload='false'/>
  <Response>my.custom.response</Response>
  <HTTPTargetConnection>
    <Properties>
      <Property name='success.codes'>2xx</Property>
    </Properties>
    <URL>http://example.com/api/something</URL>
  </HTTPTargetConnection>
</ServiceCallout>

The result of the call would be placed into a context variable called my.custom.response. Then, in Javascript you could access the JSON like so:

var body = JSON.parse(context.getVariable('my.custom.response.content')); 

This assumes that the response really is JSON. You may wish to check the content-type header in the Javascript before trying JSON.parse().

View solution in original post

6 REPLIES 6

@Sandeep Murusupalli - can you show the ServiceCallout policy please?

This is my policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout enabled="true" continueOnError="false" async="false" name="07.response.enrichment.call.partner.service">
    <FaultRules/>
    <Properties/>
    <Request clearPayload="false" ref="partner.request">partner.request</Request>
    <Response>partner.response</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>http://184.73.184.166:1984/PartnerService/v1/catalog</URL>
    </HTTPTargetConnection>
</ServiceCallout>

I want to use a jpath element from partner.response variable. But like to not have another extract variables policy to just get that

adas
New Member

You could simply do: sc_response.content

In the below example, I try to pretty print the json response received from the service callout in javascript:

var payload = JSON.parse(context.getVariable("sc_response.content"); response.content = JSON.stringify(payload,null,2) + '\n';

@arghya das can I get the response.content directly, without using context.getVariable(response.content);

@Sandeep Murusupalli no, you still need to do context.getVariable, like I have shown in the code snippet. The first comment was only meant to give you the variable name, sorry if that confused you.

Suppose you have a ServiceCallout policy like this:

<ServiceCallout name='ServiceCallout-1'>
  <Request variable='my.custom.request' clearPayload='false'/>
  <Response>my.custom.response</Response>
  <HTTPTargetConnection>
    <Properties>
      <Property name='success.codes'>2xx</Property>
    </Properties>
    <URL>http://example.com/api/something</URL>
  </HTTPTargetConnection>
</ServiceCallout>

The result of the call would be placed into a context variable called my.custom.response. Then, in Javascript you could access the JSON like so:

var body = JSON.parse(context.getVariable('my.custom.response.content')); 

This assumes that the response really is JSON. You may wish to check the content-type header in the Javascript before trying JSON.parse().