How can I add a payload to a service callout?

Not applicable

I'm trying to make a call from Apigee to slack, however it isn't adding the payload to the call.

What can I do to fix this?

Code is below:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">
    <DisplayName>Service Callout-1</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <Set>
            <Headers>
                <Header name="Authorization">{slack_token}</Header>
            </Headers>
            <PayLoad contentType="application/json" variablePrefix="@" variableSuffix="#">
                {
                    "text": "Hi from apigee"
                }
            </PayLoad>
        </Set>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>https://hooks.slack.com/services/{hook}</URL>
    </HTTPTargetConnection>
</ServiceCallout>
Solved Solved
0 3 7,537
1 ACCEPTED SOLUTION

hi @Friso Denijs, for me below works; try setting header `Content-Type` and Verb as `POST`

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="true" enabled="true" name="ServCalltoLoggly">
    <DisplayName>ServCalltoLoggly</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest1">
        <Set>
            <Headers>
                <Header name="Content-Type">application/json</Header>
            </Headers>
            <Verb>POST</Verb>
            <Payload contentType="application/json" variablePrefix="@" variableSuffix="#">
                {
                    "api" : "@apiproxy.name#",
                    "environment" : "@environment.name#",
                    "verb" : "@request.verb#",
                    "apikey" : "@_apikey#",
                    "appname" : "@_appName#",
                    "statusCode" : "@message.status.code#",
                    "requestURI" : "@_messageURI#",
                    "targetURI": "@custom_target.url#",
                  	"requestBody" : "@_requestBody#"
                }
            </Payload>
        </Set>
        <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    </Request>
    <Response>logglyresponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>http://logs-01.loggly.com/inputs/{append remaining loggly URL}</URL>
    </HTTPTargetConnection>
</ServiceCallout>

View solution in original post

3 REPLIES 3

hi @Friso Denijs, for me below works; try setting header `Content-Type` and Verb as `POST`

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="true" enabled="true" name="ServCalltoLoggly">
    <DisplayName>ServCalltoLoggly</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest1">
        <Set>
            <Headers>
                <Header name="Content-Type">application/json</Header>
            </Headers>
            <Verb>POST</Verb>
            <Payload contentType="application/json" variablePrefix="@" variableSuffix="#">
                {
                    "api" : "@apiproxy.name#",
                    "environment" : "@environment.name#",
                    "verb" : "@request.verb#",
                    "apikey" : "@_apikey#",
                    "appname" : "@_appName#",
                    "statusCode" : "@message.status.code#",
                    "requestURI" : "@_messageURI#",
                    "targetURI": "@custom_target.url#",
                  	"requestBody" : "@_requestBody#"
                }
            </Payload>
        </Set>
        <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    </Request>
    <Response>logglyresponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>http://logs-01.loggly.com/inputs/{append remaining loggly URL}</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Not applicable

Hi ,

I have created a Service Call out policy which is not returing the resposne and given error. The policy is :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="true" enabled="true" name="SC-DvlaToken">
  <DisplayName>SC-DvlaToken</DisplayName>
  <Properties/>
  <Request clearPayload="true" variable="myRequest1">
  <Set>
  <Headers>
  <!-- This is retrieve from the Key-Value from Apigee Edge -->
  <Header name="Content-Type">{Content-Type}</Header>


  </Headers>
  <Verb>POST</Verb>
  <Payload contentType="application/json" variablePrefix="@" variableSuffix="#">
  { 
  "userName": "@userName#",
  "password": "@password#" 
  }
  </Payload>

  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  </Request>
  <Response>calloutResponse</Response>
  <HTTPTargetConnection>
  <Properties/>
  <URL>https://test-api.common.beta.dvla.gov.uk/thirdparty-access/v1/authenticate</URL>
  </HTTPTargetConnection>
</ServiceCallout>

Error Is below

=============

"fault": {
  "faultstring": "Execution of ServiceCallout SC-DvlaToken failed. Reason: ResponseCode 400 is treated as error",
  "detail": {
  "errorcode": "steps.servicecallout.ExecutionFailed"
  }
  }
}

Not applicable

The key value retrieve successfully - but finally Service Callout policy is failing.

When create a new proxy and in which put the same code (Service Callout policy code- Json payload) in the Assign Message policy and use the URL of the Service Callout policy as the target of the proxy, it is working fine.

Is any particular differenece when craetin Json payload in Service Callout and Assign Message?