Got a working curl-call. How to make a RESTcall do the same.

2
Following the REST tutorial, I found that spotify's searchAPI now needs authentication. I have a working curl-call "curl -X "POST" -H "Authorization: Basic <private key here>" -d grant_type=client_credentials https://accounts.spotify.com/api/token" which is giving me the access_token that I need. But I want Mendix to do the same, but in a RESTcall-action. Having set everything as I think it should be, the action fails with "400 Bad request"   See the microflow in this modelshare for the action that fails: Call REST for access_token Can anyone tell me what is wrong? The Action looks like this: with HTTP Headers: "Authorization" : "'Basic <private key here>'" and Request: "grant_type=client_credentials"
asked
5 answers
7

Hi Tim,

I have figured this out.

A 400 error means that the REST request as set up in the call REST for access token was poorly formatted.  I downloaded your model share and the issue is that the spotify web service is looking for un-encoded form data instead of JSON or XML.  Specifically, the missing header parameter content-type=application/x-www-form-unencoded

Adding this parameter make this work in Postman and that is what CURL does by default when you do:

-d grant_type=client_credentials

I fixed this in Mendix by adding the Content-Type header key with the value 'x-www-form-unencoded' and it worked just fine afterward with the body as is.

Hope this helps,

Rob

answered
3

Hi Tim,

I do not know the exact reason for the exception that you are getting (unfortunatelly the model share does not show a lot of information about rest calls, but even then I would probably not know).

However, when I run into problems like that it helps to see what the actual message that mendix generates looks like and compare that to what comes from your curl. You can set up a free listener to capture the requests for example using https://hookbin.com/ or https://requestb.in/

Hope this helps,

Andrej 

answered
3

Tim,

 

are you able to get it working in Postman? Whenever i'm implementing a new integration, I found it easier to get it working in postman first. Usually if there is something wrong with the API call, the endpoint will return an error message in JSON that will have useful information.

 

Here is a link to the postman website: https://www.getpostman.com/

answered
1

Addition for future readers: Set the loglevel of lognode 'REST consume' in the console's log Tab 'Advanced' to Trace. This will show the REST-call Mendix requests with which parameters and headers and one logline later the response Mendix received.

answered
1

Thanks for sharing your model!

I had the same problem, but for some reason it didn't work until i added the key content-type and value 'application/x-www-form-urlencoded' (so not "unencoded") to the custom HTTP headers. Also, i had no idea the custom request template was the actual "body" of the request..

So this could come in handy for future readers aswell i guess..

answered