Need help with how to define service call out.

snowamigos
Participant II

my flow is I need to call an API to get the token and then call the actual API with that token.

I was able to get the token directly calling the token URL from postman without any issues. (screen grab below)

8379-postman-call.jpg

But when I am calling the same endpoint via apigee, I am getting 401 ( endpoint throwing 500).

I understand that I am doing something wrong in my service callout. (screen grabs below)

Postman call via proxy. (please observe I am sending my request details for the actual endpoint in the body)

8380-postman-peoxycall.jpg

Apigee Service call out

8381-apigeeproxy-setup.png

Apigee Trace Error:

8382-apigee-trace.png

Please let me know if you need more details?

Solved Solved
0 7 781
1 ACCEPTED SOLUTION

It looks to me that

  • in postman you are using a request content-type of x-www-form-urlencoded
  • while in The ServiceCallout you are using a request content-type of application/json

Have you tried to use x-www-form-urlencoded in the SC?

<ServiceCallout name='SC-1'>
  <Request>
    <Set>
     <Headers>
       <Header name='content-type'>application/x-www-form-urlencoded</Header>
     </Headers>
     <FormParams>
       <FormParam name='username'>godino@google.com</FormParam>
       <FormParam name='password'>ILoveAPIs</FormParam>
       <FormParam name='grant_type'>password</FormParam>
       <FormParam name='client_id'>ABCDEF</FormParam>
       <FormParam name='client_secret'>1234567</FormParam>
     </FormParams>
     <Verb>POST</Verb>
    </Set>
  </Request>
  <Response>SNPOCAuthResponse</Response>
  <HTTPTargetConnection>
    <SSLInfo>
        <Enabled>true</Enabled>
        <IgnoreValidationErrors>true</IgnoreValidationErrors>
    </SSLInfo>
    <Properties>
      <Property name='success.codes'>2xx, 3xx, 4xx, 5xx</Property>
    </Properties>
    <URL>https://myserver.com/oauth_token.do</URL>
  </HTTPTargetConnection>
</ServiceCallout>

Anything inside the FormParam text element is treated as a message template, so you can refer to context variables by wrapping them in curlies like this:

       <FormParam name='username'>{my_username}</FormParam>

View solution in original post

7 REPLIES 7

It looks to me that

  • in postman you are using a request content-type of x-www-form-urlencoded
  • while in The ServiceCallout you are using a request content-type of application/json

Have you tried to use x-www-form-urlencoded in the SC?

<ServiceCallout name='SC-1'>
  <Request>
    <Set>
     <Headers>
       <Header name='content-type'>application/x-www-form-urlencoded</Header>
     </Headers>
     <FormParams>
       <FormParam name='username'>godino@google.com</FormParam>
       <FormParam name='password'>ILoveAPIs</FormParam>
       <FormParam name='grant_type'>password</FormParam>
       <FormParam name='client_id'>ABCDEF</FormParam>
       <FormParam name='client_secret'>1234567</FormParam>
     </FormParams>
     <Verb>POST</Verb>
    </Set>
  </Request>
  <Response>SNPOCAuthResponse</Response>
  <HTTPTargetConnection>
    <SSLInfo>
        <Enabled>true</Enabled>
        <IgnoreValidationErrors>true</IgnoreValidationErrors>
    </SSLInfo>
    <Properties>
      <Property name='success.codes'>2xx, 3xx, 4xx, 5xx</Property>
    </Properties>
    <URL>https://myserver.com/oauth_token.do</URL>
  </HTTPTargetConnection>
</ServiceCallout>

Anything inside the FormParam text element is treated as a message template, so you can refer to context variables by wrapping them in curlies like this:

       <FormParam name='username'>{my_username}</FormParam>

@Dino-at-Google it worked, at least in my trace I can see the access token and refresh token.

but I have a new issue, See the error below

Where do I configure to pass content-type: JSON for my actual endpoint?

{
    "error": {
        "message": "Invalid content-type. Supported request media types for this service are: [application/json, application/xml, text/xml]",
        "detail": null
    },
    "status": "failure"
}

I am getting below error when I am trying to remove the header from Assign Message flow.

8383-assignmessage-policy.jpg

it should be,

<Remove>
    <Headers>
      <Header name="header_name">header_value</Header>
      ...
    </Headers>
</Remove>

No, Remove is like this:

<Remove>
    <Headers>
      <Header name="header_name"/>
      ...
    </Headers>
</Remove>

Also, you don''t need Remove at all if you you use Set rather than Add.

<AssignMessage name="AssignMessageSNPOC">
  <Set>
    <Headers>
      <Header name="content-type">application/json</Header>
       ...
    </Headers>
     ...
  </Set>
  ...  
</AssignMessage>

Also, if you are setting the content-type, you will also want to set the payload.

I don't suppose the payload is just magically application/json, and only the header is in need of being set.

Thanks @Siddharth Barahalikar . I have made the suggested changes.

API is still returning Invalid content-type error.

{
    "error": {
        "message": "Invalid content-type. Supported request media types for this service are: [application/json, application/xml, text/xml]",
        "detail": null
    },
    "status": "failure"
}

Below is my modified assign message policy, Please let me know if you need any other details.

8384-new-assignpolicy.jpg


In the future, Please open a new question to ask a new question.

Thanks, Will do that.