libUrlMultipartFormData on android error

The place to discuss anything and everything about running your LiveCode on Android

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

libUrlMultipartFormData on android error

Post by BigameOver » Thu Oct 01, 2020 4:37 pm

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

Klaus
Posts: 12206
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: libUrlMultipartFormData on android error

Post by Klaus » Thu Oct 01, 2020 4:52 pm

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.

BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

Re: libUrlMultipartFormData on android error

Post by BigameOver » Fri Oct 02, 2020 8:27 am

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.

Klaus
Posts: 12206
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: libUrlMultipartFormData on android error

Post by Klaus » Fri Oct 02, 2020 12:24 pm

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).

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 826
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: libUrlMultipartFormData on android error

Post by Thierry » 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
Thierry Douez
- Senior computer scientist
- Conseil, dévelopement et support en Français
https://sunny-tdz.com
https://sunny-tdz.com/livecode/externals

BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

Re: libUrlMultipartFormData on android error

Post by BigameOver » Fri Oct 02, 2020 6:02 pm

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

BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

Re: libUrlMultipartFormData on android error

Post by BigameOver » Fri Oct 02, 2020 6:03 pm

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

mimu
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 72
Joined: Tue Mar 05, 2013 7:00 pm
Location: Berlin
Contact:

Re: libUrlMultipartFormData on android error

Post by mimu » Fri Oct 02, 2020 10:57 pm

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
Attachments
imgbbUpload_mobile.livecode.zip
(4.18 KiB) Downloaded 39 times

BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

Re: libUrlMultipartFormData on android error

Post by BigameOver » Mon Oct 05, 2020 9:49 am

Thank you! It works perfect :D

Post Reply

Return to “Android Deployment”