Looking for help with CURL form request (on mobile)

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

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

Looking for help with CURL form request (on mobile)

Post by simon.schvartzman » Tue Oct 19, 2021 8:57 pm

Hi all, I hit the wall (again) trying to request a CURL that uses form syntax...

The original CURL I'm trying to submit is like this

Code: Select all

curl --location --request POST 'https://XXX.com/api/v2/photos/' \
--header 'Authorization: Bearer YYY' \
--form 'visit_id="custom_visit_141020214319311"' \
--form 'photo_id="14102021190755-1999"' \
--form 'photo_data=@"/C:/Users/alex_/Downloads/2021-10-07-07-18-44-5424-o.jpg"' \
--form 'scene_id="6168005a6a929-9084"'
and this is my code so far

Code: Select all

on mouseUp
   
   put "YYY" into myToken
   
   -- build the header(s)
   put empty into tHeader
   put "Authorization:Bearer " & myToken  into tHeader
   set the httpHeaders to tHeader
   
   -- load the image data
   put "/Users/simonschvartzman/Downloads/Pic1.jpg" into theFilePath
   put URL ("binfile:" & theFilePath) into theBinaryData
   
    -- build the body
   put quote & "visit_id" & quote & ":" & quote & "custom_visit_141020214319311-1" & quote & "," & CR into tMsg
   put quote & "photo_id" & quote & ":" & quote & "14102021190755-1999-1" & quote & "," & CR after tMsg
   put quote & "photo_data" & quote & ":" & quote & "@/Users/simonschvartzman/Downloads/Pic1.jpg" & quote & "," & CR after tMsg
   put quote & "scene_id" & quote & ":" & quote & "6168005a6a929-9084-1" & quote after tMsg
   put  tMsg into tArgList 
   put  "{" & tMsg & "}" into tArgList 
       
   -- set the URL for the photo method
   put "https://XXX.com/api/v2/photos/" into tURL --the url
   
   -- fire the request
   post tArgList to url tURL
   
   -- get the result
   put it into tResponse
   
   put tResponse
   
end mouseUp 
and this is the error I'm getting
{"type":"error","message":"3 validation errors for Request\nbody -> photo_id\n field required (type=value_error.missing)\nbody -> visit_id\n field required (type=value_error.missing)\nbody -> photo_data\n field required (type=value_error.missing)","status":422}
I know photo_data is not properly set but for now I don't care since first I have to get rid of the other two errors

ps: since my end goal is to deploy in mobile using libURLFormData doesn't apply...

Anyone willing/able to help? Many thanks
Simon
________________________________________
To ";" or not to ";" that is the question

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Looking for help with CURL form request (on mobile)

Post by mwieder » Sat Oct 30, 2021 6:38 pm

Simon-

Looks to me like you're trying to put pseudo-JSON into a REST api that wants form-encoded data.
If the curl syntax you have there works then you might try putting the into

visit_id="custom_visit_141020214319311"
photo_id="14102021190755-1999"
etc

instead of

{
"visit_id":"custom_visit_141020214319311",
"photo_id":"14102021190755-1999"
}

...and as always, Postman is your friend for figuring out REST api syntax.
https://www.postman.com/

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

Re: Looking for help with CURL form request (on mobile)

Post by simon.schvartzman » Sat Oct 30, 2021 9:33 pm

@mwieder many thanks for your input, I used exactly your suggested approach and achieved the expected result.

And yes, as you said Postman is indeed of great help.

Best
Simon
________________________________________
To ";" or not to ";" that is the question

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”