Windows App calling to JSON Rest ApI. Example please

Hi

Can anyone point me to a good example for Rest Api with JSON on Windows.
I found RestY and Curls MBS and many topics at this forum, but I still wasn’t able to send a good post to the api. Everytime I got an error telling me I did not send any username and password.
Testing in an online APItest is working good,That means I’m using the correct username and password.

I know I have to use an URL (endpoint).
At first I have to POST a JSON string with username and password. Then I must receive a token.
After getting that token the second step is, calling the api again on a slightly different URL containing that token in the JSON string and uploading a file.

https://www.thisismyurl.com/api/token-auth/
Type = POST
{“username”: “admin”,“password”: “PW123456”}

  • URL and password for example
dim c as new CURLSMBS

c.OptionPostFields = "{""username"": ""admin"",""password"": ""PW123456""}"
c.OptionPost = true
c.OptionURL = "https://www.thisismyurl.com/api/token-auth/ "

Then call c.Perform and check c.DebugData and c.OutputData.

Thanks Christian,

I had already tried this exactly like this, before asking my question.
But the result was a message telling me I did not send any username and password.
Because you came with the same code I used during my tryout. I was pointed to do something additional.

I found the solution. I need a header Content-Type: application/json.

c.SetOptionHTTPHeader array("Content-Type: application/json")

Now it is working fine.

Complete code

use MBS plugin Curl

   '  TEST REST API (json)
dim strPostfields as string
dim strURL as string
dim c as new CURLSMBS

strPostFields = "{""username"": ""admin"",""password"": ""Pw123456""}"
strURL = "https://www.thisismyurl.nl/api/token-auth/ "
c.SetOptionHTTPHeader array("Content-Type: application/json")
c.CollectDebugData = True
c.CollectOutputData = true
c.OptionPostFields = strPostfields
c.OptionPost = true
c.OptionURL = strURL  

lblPerformResultcode=str(c.Perform)

'Output
lblDebugData.text=ReplaceLineEndings(c.DebugData,EndOfLine)
lblResultData.text=ReplaceLineEndings(c.OutputData,EndOfLine)
1 Like