HTTP multipart forms
Posted: 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
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