Page 1 of 1

libUrlMultipartFormData on android error

Posted: Thu Oct 01, 2020 4:37 pm
by BigameOver
Does the libUrlMultipartFormData support android?
I need to upload an image I captured on camera from the android device.
When I try to run it on android it doesn't work but when I do it on pc it does work.
Thank you

Re: libUrlMultipartFormData on android error

Posted: Thu Oct 01, 2020 4:52 pm
by Klaus
Always check the dictionary! 8)

Il will list all platforms where a command/function is supported or not!
For "libURLMultipartFormData" it lists: mac, windows, linux, html5
so you are out of luck.

But take a look at the resulting FORM in the IDE, should be not to hard to "rebuild" that one,
it is just text in the end.

Re: libUrlMultipartFormData on android error

Posted: Fri Oct 02, 2020 8:27 am
by BigameOver
Klaus wrote:
Thu Oct 01, 2020 4:52 pm

But take a look at the resulting FORM in the IDE, should be not to hard to "rebuild" that one,
it is just text in the end.
How do I build a similar function?
I tried to replace the parts with the binfile that doesn't work but it still doesn't work.

Re: libUrlMultipartFormData on android error

Posted: Fri Oct 02, 2020 12:24 pm
by Klaus
BigameOver wrote:
Fri Oct 02, 2020 8:27 am
How do I build a similar function?
I did not mean to create a similar function, but to re-create the FORM that this command creates in the IDE.
BigameOver wrote:
Fri Oct 02, 2020 8:27 am
I tried to replace the parts with the binfile that doesn't work but it still doesn't work.
What did you try so far?
Please show us the script(s).

Re: libUrlMultipartFormData on android error

Posted: Fri Oct 02, 2020 1:28 pm
by Thierry
BigameOver wrote:
Fri Oct 02, 2020 8:27 am
take a look at the resulting FORM in the IDE, should be not to hard to "rebuild" that one,
How do I build a similar function?
I tried to replace the parts with the binfile that doesn't work but it still doesn't work.
Hi,

following Klaus idea....

1) get this interesting stack from minu:

viewtopic.php?f=11&t=34705&p=196809#p196802

2) find in the stack script those lines:

Code: Select all

      set the httpHeaders to line 1 of tForm
      post line 2 to -1 of tForm to url tUrl
3) set a breakpoint to the 2nd line and read the form text with the debugger.

HTH,

Thierry

Re: libUrlMultipartFormData on android error

Posted: Fri Oct 02, 2020 6:02 pm
by BigameOver
Thierry wrote:
Fri Oct 02, 2020 1:28 pm
BigameOver wrote:
Fri Oct 02, 2020 8:27 am
take a look at the resulting FORM in the IDE, should be not to hard to "rebuild" that one,
How do I build a similar function?
I tried to replace the parts with the binfile that doesn't work but it still doesn't work.
Hi,

following Klaus idea....

1) get this interesting stack from minu:

viewtopic.php?f=11&t=34705&p=196809#p196802

2) find in the stack script those lines:

Code: Select all

      set the httpHeaders to line 1 of tForm
      post line 2 to -1 of tForm to url tUrl
3) set a breakpoint to the 2nd line and read the form text with the debugger.

HTH,

Thierry
I checked this out and it returns some things that I dont understand like boundary, etc

Re: libUrlMultipartFormData on android error

Posted: Fri Oct 02, 2020 6:03 pm
by BigameOver
Klaus wrote:
Fri Oct 02, 2020 12:24 pm
What did you try so far?
Please show us the script(s).

Code: Select all

\
function libUrlMultipartFormDataAndroid @pFormData, pParam
local tNumParams,tNumParts,tBoundary,tKeys,tKey,tValue,tPart
   local tFile,tNumFiles,tFilepath,tFilename,tSubBoundary
   
   put the paramcount into tNumParams
   if tNumParams < 1 then return "error Insufficient parameters"
   put (tNumParams - 1) div 2 into tNumParts
   
   put empty into pFormData ##ensure it is empty
   answer "5.1"
   ##create initial boundary
   put "__Part__" into tBoundary
   put ulFormBoundary() after tBoundary
   
   put "Content-type: multipart/form-data; boundary=" & quote & tBoundary & quote & return into pFormData
   answer "5.12"
   if tNumParams < 2 then
      ## do nothing for now, this will create empty shell for use with libUrlMultipartFormAddPart
   else if tNumParams = 2 then ##treat as array
      put keys(pParam) into tKeys
      sort tKeys numeric
      repeat for each line tKey in tKeys
         put "--" & tBoundary & CRLF after pFormData
         put "Content-Disposition: form-data; name=" & quote & item 1 of pParam[tKey] & quote after pFormData
         put item 2 to -1 of pParam[tKey] into tValue
         if char 1 to 6 of tValue = "<file>" then  
            put tValue into tFile
            put empty into tValue
            put char 7 to -1 of tFile into tFilepath
            set the itemDel to "/"
            put item -1 of tFilepath into tFilename
            set the itemDel to comma
            put ";" && "filename=" & quote & tFilename & quote after pFormData
            put CRLF & "Content-Type: application/octet-stream" after pFormData
            put tFile into tValue
            answer "5.13"
            if the result <> empty then
               return "error" && the result
            end if
         end if
         put CRLF & CRLF after pFormData
         put tValue after pFormData
         put CRLF after pFormData
         
      end repeat
      answer "5.2"
   else if tNumParts > 0 then ##treat as key/value pairs
      put 0 into tPart
      repeat with i = 2 to tNumParams
         
         if i mod 2 = 0 then ##stage 1 of part
            add 1 to tPart
            if tPart > tNumParts then exit repeat
            put "--" & tBoundary & CRLF after pFormData
            put "Content-Disposition: form-data; name=" & quote & param(i) & quote after pFormData
            
         else
            put param(i) into tValue
            answer "5.5"
            if char 1 to 6 of tValue = "<file>" then
               put tValue into tFile
               put empty into tValue
               put char 7 to -1 of tFile into tFilepath            
               set the itemDel to "/"
               put item -1 of tFilepath into tFilename
               set the itemDel to comma
               put ";" && "filename=" & quote & tFilename & quote after pFormData
               put CRLF & "Content-Type: application/octet-stream" after pFormData
               put tFile into tValue
               if the result <> empty then
                  return "error" && the result
               end if
            end if
            put CRLF & CRLF after pFormData
            put tValue after pFormData
            put CRLF after pFormData
         end if
      end repeat
      
   end if
   
   put  "--" & tBoundary & "--" after pFormData ##end boundary
   return empty
   
end libUrlMultipartFormDataAndroid

Re: libUrlMultipartFormData on android error

Posted: Fri Oct 02, 2020 10:57 pm
by mimu
Here you will find how multipart form-urlencoded data is structured:
https://dev.to/getd/x-www-form-urlencod ... -mins-5hk6

i' ve attached a samplestack to upload images to ImgBB
without using libUrlMultipartFormData.
So it should work on mobile

1. Set HttpHeader

Code: Select all

 put "Content-type: multipart/form-data; " into theader
   put "boundary=" & quote & "__Part__" & quote after theader
   set the httpHeaders to theader
2. prepare post data

Code: Select all

function prepPostDatafromPath pimage
   local tdata,tfilename,timageData
   put  url ("binfile:" & pimage) into timageData
   set the itemdelimiter to slash
   put last item of pimage into tfilename
   put "--__Part__" & crlf into tdata
   put "content-Disposition: form-data; name=" & quote & "image" & quote & ";filename=" & quote & tfilename & quote & crlf after tdata
   put "content-Type: application/octet-stream" & crlf & crlf after tdata
   put timageData & crlf after tdata
   put "--__Part__--" after tdata
   return tdata
end prepPostDatafromPath
3. post data to url

Code: Select all

put prepPostDatafromPath(pLocalFilePath) into tpostData
post tpostData to url tUrl

Re: libUrlMultipartFormData on android error

Posted: Mon Oct 05, 2020 9:49 am
by BigameOver
Thank you! It works perfect :D

Re: libUrlMultipartFormData on android error

Posted: Fri Oct 22, 2021 10:35 pm
by simon.schvartzman
Hi guys, this is really a great script.

I wonder if you have any example showing how to send some parameters (Keys=values) on top of the image itself.

I want to use the approach used here to send (in mobile) some metadata (like where the picture was taken, the date, etc...) together with the picture ...

Many thanks

Re: libUrlMultipartFormData on android error

Posted: Sun Oct 24, 2021 9:53 pm
by simon.schvartzman
According to what I've found, and contrary to what is written in the dictionary, at least when the stand alone is built with 9.6.5 libUrlMultipartFormData works fine both on Android and iOS.

Anyone else has tested?