Construction file upload payload
I am using Google Apps Script to upload files, which uses UrlFetchApp to construct the headers and the payload JSON.
In the docs, I can't find a way to construct this data. What does podio expect?
This is what I am POST-ing now, but it is not right.
File is a fileblob / binairy.
var data = {
'source':file,
'filename':file.getName()
}
var params = {
"method": "post",
"payload": data,
"contentType": "multipart/form-data",
"headers":
{
"Authorization": "OAuth2 "+access_token,
"Content-type": "multipart/form-data"
},
"muteHttpExceptions": true
};
var res = UrlFetchApp.fetch(url,params).getContentText() // do the POST
-
This is the request I send to Podio. I am creating a proper multipart/formdata request in the background as you see the result in the payload here:
{
"headers": {
"Authorization": "OAuth2 xxx",
"ContentType": "multipart/form-data; boundary=\"0.92d6937d51984\""
},
"useIntranet": false,
"followRedirects": true,
"payload": "--0.92d6937d51984\r\nContent-Type: application/pdf\r\nContent-Transfer-Encoding: binary\r\n\r\nContent-Disposition: form-data; source=\"filename.pdf\"; name=\"my_file.bin\"\r\nxxxxxxxxxxxxxxxx\r\n--0.92d6937d51984--",
"method": "post",
"contentType": "multipart/form-data; boundary=\"0.92d6937d51984\"",
"validateHttpsCertificates": true,
"url": "https://api.podio.com/file/v2/"
}This is the error I recieve:
{
"error_parameters": {},
"error_detail": null,
"error_propagate": false,
"request": {
"url": "http://api.podio.com/file/v2/",
"query_string": "",
"method": "POST"
},
"error_description": "No matching operation could be found. Missing parameters: source.",
"error": "not_found"
} -
This is using echoservice what my request looks like:
POST / HTTP/1.0
Host: echo.200please.com
Connection: close
Content-Length: 199
Authorization: OAuth2 xxxxx
Content-Type: multipart/form-data; boundary="0.73e36f726ffb2"
ContentType: multipart/form-data; boundary="0.73e36f726ffb2"
User-Agent: Mozilla/5.0 (compatible; GoogleApps script; +http://script.google.com/bot.html)
Accept-Encoding: gzip,deflate--0.73e36f726ffb2
Content-Type: application/pdf
Content-Transfer-Encoding: binaryContent-Disposition: form-data; source="filename.pdf"; name="my_file.bin"
xxxxxxxxxxxxxxxx
--0.73e36f726ffb2--" -
I am still trying, getting a bit further.
Getting the next error:
"Invalid value null (null): must be non empty string"
The HTTP POST payload is:
POST / HTTP/1.0
Host: echo.200please.com
Connection: close
Content-Length: 192
Authorization: OAuth2 cccc
Content-Type: multipart/form-data; boundary="0.aaead71c4823f8"
User-Agent: Mozilla/5.0 (compatible; GoogleApps script; +http://script.google.com/bot.html)
Accept-Encoding: gzip,deflate
--0.aaead71c4823f8
Content-Disposition: form-data; name="source"; filename="test.pdf";
Content-Type: application/pdf
Content-Transfer-Encoding: binary
xxxxxdataEOF
--0.aaead71c4823f8-- -
It's been a very long time since I've created a multipart request manually, but I believe Podio requires two parts to be present. One part is the file data (which you have). The other part is the file name which I don't see. So get the filename added as the second part of your multipart request and I believe things should work.
-
Still getting some errors. Is there an example of a working dump of the multipart request to compare?
--0.2e64e61d88552
Content-Disposition: form-data; name="source";
Content-Type: application/pdf
Content-Transfer-Encoding: binary
%PDF-1.4 %���� 2 0 obj <</Type /Catalog /Pages 1 0 R >> endobj 3 0 obj <</Type /Page /Parent 1 0 R EOF
--0.2e64e61d88552--
Content-Disposition: form-data; name="filename";
test.pdf -
I am still struggling uploading files.
This is the data I send to Podio, but I get an internal server error:
Headers:
Remote Address:54.229.15.211:443
Request URL:https://api.podio.com/file/v2/
Request Method:POST
Status Code:500 Internal Server Error
Request Headersview source
Accept-Encoding:gzip,deflate,sdch
Accept-Language:nl
Authorization:OAuth2 4dd78aaf3ae74c3ba5860792dfd07c14
Connection:keep-alive
Content-Length:6687
Content-Type:multipart/form-data; boundary=END_OF_PART_ABCDEFG; charset=UTF-8
Host:api.podio.com
Origin:chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
User-Agent:Mozilla/5.0 (X11; CrOS x86_64 5841.98.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36payload:
Content-type: multipart/form-data; boundary=END_OF_PART_ABCDEFG
--END_OF_PART_ABCDEFG
Content-Disposition: form-data; name="source"; filename="/text/plain.txt"
Content-Type: text/plaintextdata
--END_OF_PART_ABCDEFG
Content-Disposition: form-data; name="filename"text.txt
--END_OF_PART_ABCDEFGWhy is this error?
-
Few questions:
Should the payload be bytearray?
Is mimetype always application/octet-stream?
Is a filename needed with the source-boject?I only run into server 500 errors when trying every option to think about, and Podio only documents uploading via PHP. It should be easy to do a multipart/POST request from any system...
Is it possible to send a JSON object with a bytearray maybe instead? I am really generating big issues not being able to attach files using Google Apps Script ;)
-
I got it working.
The payload should be a byte-array. Converted to string it is:
--PodioGASBoundary_FileUpload
Content-Disposition: form-data; name="filename"filename.txt
--PodioGASBoundary_FileUpload
Content-Disposition: form-data; name="source"; filename="filename.txt"
Content-Type: text/plainThis is a text file
--PodioGASBoundary_FileUpload--Headers need to have "multipart/form-data; boundary=PodioGASBoundary_FileUpload" as ContentType, and there should be a ContentLength header.
-
Andrew, please mail me at riel@zzapps.nl :)
Please sign in to leave a comment.
Comments
10 comments