HTTP multipart forms

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bmatichuk
Posts: 15
Joined: Tue Oct 12, 2010 7:19 am
Location: Edmonton

HTTP multipart forms

Post by bmatichuk » Wed Aug 01, 2012 10:10 am

You can't use the liburl library on the iPhone, so you have to construct your own multipart forms. This seems to work in LiveCode desktop, but does not work on the iphone.

The killnewlines is required because otherwise, the file is messed up. the base64 encoder adds them in.

function killnewlines theText
repeat with i = the number of chars of theText down to 1
if char i of theText is linefeed then
delete char i of theText
end if
end repeat
return theText
end killnewlines

function post1
local theImage
export image "testimage" to theImage as JPEG
local theHeader, theBody
put "--__Part__" & crlf into theBody
put "content-Disposition: form-data; name=" & quote & "apiUserToken" & quote & crlf & crlf after theBody
put "2fAhc8tzTsF1BxVX2GnaJ8VfdvX1Nn" & crlf after theBody
put "--__Part__" & crlf after theBody
put "content-Disposition: form-data; name=" & quote & "sbxUserToken" & quote & crlf & crlf after theBody
put "2e763f13519f55f3dad49" & crlf after theBody
put "--__Part__" & crlf after theBody
put "content-Disposition: form-data; name=" & quote & "imageType" & quote & crlf & crlf after theBody
put "receipt" & crlf after theBody
put "--__Part__" & crlf after theBody
put "content-Disposition: form-data; name=" & quote & "inserterId" & quote & crlf & crlf after theBody
put "MyCompany" & crlf after theBody
put "--__Part__" & crlf after theBody
put "content-Disposition: form-data; name=" & quote & "images" & quote & ";filename=" & quote & "dummy.jpg" & quote & crlf after theBody
put "content-Type: application/octet-stream" & crlf after theBody
put "content-transfer-encoding: base64" & crlf & crlf after theBody
put killnewlines(base64encode(theImage)) & crlf after theBody
put "--__Part__--" after theBody
put "content-type: multipart/form-data; boundary=" & quote & "__Part__" & quote & crlf after theHeader
set the httpHeaders to theHeader
return theBody
end post1

localuploadShoeboxedURL
put "https://api.shoeboxed.com/v1/ws/api-upload.htm" into uploadShoeboxedURL

local theBody
put post1() into theBody
post theBody to URL uploadShoeboxedURL

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: HTTP multipart forms

Post by shaosean » Thu Aug 02, 2012 5:04 am

Code: Select all

function killnewlines theText
repeat with i = the number of chars of theText down to 1
if char i of theText is linefeed then
delete char i of theText
end if
end repeat
return theText
end killnewlines
or even quicker

Code: Select all

function killnewlines theText
  replace LF with EMPTY in theText
  return theText
end killnewlines

bmatichuk
Posts: 15
Joined: Tue Oct 12, 2010 7:19 am
Location: Edmonton

Re: HTTP multipart forms

Post by bmatichuk » Wed Aug 08, 2012 11:54 am

Here is a an important update!!

The above code will not work on the iphone because of a header format issue.
I sent the code to LiveCode support and they kindly debugged it for me.
Here is the response they sent (I tried it and it works. Yaaay!!)


The reason for your issue is a slight bug in your code. The parsing of your header is done by the platform rather that LiveCode so as a result you see slight differences between desktop and iOS. iOS is strict to the letter of the law, whereas Desktop header parsing is a little more forgiving.

The following line of code is the culprit, and it particular to "crlf":

put "content-type: multipart/form-data; boundary=" & quote & "__Part__" & quote & crlf after theHeader

Should look like:

put "content-type: multipart/form-data; boundary=" & quote & "__Part__" & quote after theHeader

Post Reply

Return to “Internet”