tsNetPost how to format the curl

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
trevix
Posts: 958
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

tsNetPost how to format the curl

Post by trevix » Tue May 25, 2021 11:27 am

I have been asked to implement a web login from LC, but my ignorance on tsNet and curl is enourmus. So please bare with me.

The cUrl I have been supplied is this:

Code: Select all

curl -X 'POST' \
  'https://xxxx.azurewebsites.net/api/Authenticate/Login' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "email": "user@example.com",
  "password": "string"
}'
the request URL is something like this:

Code: Select all

https://xxxx.azurewebsites.net/api/Authenticate/Login
I took this code from the LC lesson but I always get a "400" or "415" response from pStatus:

Code: Select all

on mouseUp pMouseButton
     local tUrl, tPostData, tHeaders, tError
     tsNetInit --initialize the tsnet library
     put fld "FldEmail" into tEmail
     put fld "FldPassword" into tPassword
     put fld "FldURL" into tURL
     --post  json
     put quote & "email" & quote & ":" & quote & tEmail & quote into tPostData
     put CRLF & quote & "password" & quote & ":" & quote & tPassword & quote  after tPostData
     put "{" & tPostData & "}" into tPostData
     put "Accept:*=Content-Type: application/json" into tHeaders
     put tsNetPost("connectionA",tUrl,tHeaders,tPostData,"postResponse") into tError
end mouseUp

on postResponse pID, pStatus, pBytes, pResult
     local tError, tData, tResponse
     
     if pResult is not 0 then
          -- If pResult is not 0, then a libcurl error occurred, call tsNetRetrError() to get more detail of the error
          put tsNetRetrError(pID) into tError
          answer "Error:" && tError && "while retrieving data for entry" & pID
     else if the first char of pStatus is not 2 then
          -- Successful HTTP transactions get a 2xx response code, so error if this is not the case
          answer "Received HTTP response code" && pStatus && "while retreiving data for entry" & pID
     else
          -- Otherwise we have a successful transaction, so retrieve any data that was returned
          put tsNetRetrData(pID, tError) into tData
          if tError is not empty then
               -- This should only happen if the external ran out of memory retrieving the data, or the connection was already closed
               answer "Error:" && tError && "while retrieving data for entry" & pID
          end if
     end if
     
     -- Always close the connection to release any memory allocated to the connection
     tsNetCloseConn pID
     if tData is not empty then
          answer tData
     end if
end postResponse
What am i doing wrong?
Thanks
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: tsNetPost how to format the curl

Post by jacque » Tue May 25, 2021 8:57 pm

A 415 error indicates wrong encoding. I've been using these two handlers with good results:

Code: Select all

function arrayToJSON pDataArray -- convert LC array to JSON for server
  -- pDataArray = LC array of values
  put jsonExport(pDataArray) into tJSON
  return tJSON
end arrayToJSON

function JSONToArray pServerParams -- convert server JSON to LC array
  -- pServerParams = JSON array from server
  put jsonImport(pServerParams) into tJSON
  return tJSON
end JSONToArray
The jsonImport/Export commands do all the necessary work. A login array could just be:

Code: Select all

tArray
  name: Joe Smith
  password: myPassword
For headers:

Code: Select all

put "Content-Type: application/json" & cr & "Accept: application/json" into tHeaders
set the httpHeaders to tHeaders
So after you've got a LC array, it's basically it's like this:

Code: Select all

put arrayToJSON(pValueArray) into tServerString
put "Content-Type: application/json" & cr & "Accept: application/json" into tHeaders
set the httpHeaders to tHeaders
post tServerString to url tURL
put the result into tNetworkErr -- status and/or errors show here
put it into tData -- the content returned, if any
...
if tNetworkErr <> "" and tData = empty then -- never reached the server
    -- handle the error
else -- server response, JSON
    if tData <> "" then
      put textDecode(tData,"UTF8") into tData
      put JSONToArray(tData) into tData
   end if
end if -- now you have an array to work with
The JSON library also includes JSONToArray and arrayToJSON but I had better luck with JSONImport/JSONExport. (I'm still not sure why I need to textDecode the incoming data but I don't need to textEncode the outgoing data. But it works.)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

trevix
Posts: 958
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: tsNetPost how to format the curl

Post by trevix » Wed May 26, 2021 9:52 am

Thanks: your solution works perfectly.
I was hoping to use a non blocking tsNetPost instead of a simple post.
Do you have a solution for that too?
And, more important, does it make sense to do a non-blocking web Login?
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

trevix
Posts: 958
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: tsNetPost how to format the curl

Post by trevix » Wed May 26, 2021 10:06 am

Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: tsNetPost how to format the curl

Post by jacque » Wed May 26, 2021 6:42 pm

Sorry, I've never needed a non-blocking login so I don't have an example. In all the apps where I've used this, the user can't do anything else until login is complete. And we've never had an instance where the server didn't return a response immediately. Login verification has always happened instantly.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Internet”