Help with CURL request...

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
simon.schvartzman
Posts: 638
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Help with CURL request...

Post by simon.schvartzman » Mon Oct 18, 2021 10:14 pm

Hi all, first time trying to handle a CURL request with LC and after hitting the wall (and reading several posts and dictionary entries) for several hours I decided to ask for your kind and customary help

This is the CURL I need to post in order to get an authorization token
curl -X 'POST' \
'https://XXXXXXX.com/api/v2/auth/login' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"login": "YYYYYYYYY",
password": "ZZZZZZZZ"
}'
This is the code I've put together (which seems to be promising :D to me)

Code: Select all

on mouseUp
   local tHeaders, tResult, tBytes, tRecvHeaders, tPostData, tData
   local tSettings   
   
   put "accept: application/json" & cr into tHeader
   put "Content-Type: application/json" & cr after tHeader
   
   put "" into tPostData
   
   put "https://XXXXXXX.com/api/v2/auth/login" into tURL --the url
   
   put "YYYYYYYYY" into tSettings["username"]
   put "ZZZZZZZZ" into tSettings["password"]
   
   put tsNetPostSync(tURL, tHeaders, tPostData, tRecvHeaders, tResult, tBytes, tSettings) into tData
   
   put tData
   
end mouseUp
and this is the error I'm getting
{"type":"error","message":"1 validation error for Request\nbody\n field required (type=value_error.missing)","status":422}
Many thanks
Simon
________________________________________
To ";" or not to ";" that is the question

simon.schvartzman
Posts: 638
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: Help with CURL request...(SOLVED)

Post by simon.schvartzman » Tue Oct 19, 2021 10:29 am

I got it working...it maybe helpful for others so here it goes

Code: Select all

put "accept: application/json" & cr into tHeader
   put "Content-Type: application/json" & cr after tHeader
   set the httpHeaders to tHeaders
      
   put quote & "login" & quote & ":" & quote & "YYYYYYYYY" & quote & "," & CR into tMsg
   put quote & "password" & quote & ":" & quote & "ZZZZZZZZ" & quote after tMsg
   put  "{" & tMsg & "}" into tArgList 
   set text of field "TheData" to tArgList 
   
   put "XXXXXXX.com" into tURL --the url
   
   post tArgList to url tURL
   
   put it into tResponse
Simon
________________________________________
To ";" or not to ";" that is the question

Post Reply

Return to “Talking LiveCode”