request body stream exhausted error

847 views
Skip to first unread message

Raymond Camden

unread,
Nov 23, 2012, 10:40:51 PM11/23/12
to phon...@googlegroups.com
I'm trying to do a file upload to Parse.com using the FileTransfer object. I've got this code as part of the camera select picture handler:

function gotPic(p) {

var options = new FileUploadOptions();
options.fileName=p.substr(p.lastIndexOf('/')+1);
options.mimeType = "image/jpeg";

options.params = {};
options.params.headers = {
"X-Parse-Application-Id":PARSE_APP,
"X-Parse-REST-API-Key":PARSE_REST,
"Content-Type": "image/jpeg"
};

var ft = new FileTransfer();
ft.upload(p, "https://api.parse.com/1/files/"+options.fileName, function(e) {
console.log('success');
}, generalError, options);


}

On upload, I get: 

FileTransferError {

    code = 3;

    "http_status" = 0;

    source = "file://localhost/var/mobile/Applications/AAA7A602-61F1-46B2-BD2E-960E446AD4A0/tmp/cdv_photo_009.jpg";

    target = "https://api.parse.com/1/files/cdv_photo_009.jpg";

}

2012-11-23 21:38:20.430 filexferparse[13563:907] File Transfer Error: request body stream exhausted


Any ideas?

Raymond Camden

unread,
Nov 23, 2012, 10:58:04 PM11/23/12
to phon...@googlegroups.com
Ok, got it. I was using options.params.headers to specify Parse.com headers. But it should have been options.headers.

This example is in the docs - isn't this wrong?

var params = {};
params.headers={'headerParam':'headerValue'};

options
.params = params;

Simon MacDonald

unread,
Nov 26, 2012, 12:06:30 AM11/26/12
to phonegap
It's actually a couple of things. One the docs should be updated since that is the old way of specifying headers. Two, there is a bug in the Android code for backward compatibility. See if you can spot it:

// Look for headers on the params map for backwards compatibility with older Cordova versions.

final JSONObject headers = args.optJSONObject(8) == null ? params.optJSONObject("headers") : args.optJSONObject(8);

I bet you didn't find it. The first comparison against null will fail. You see when the arguments are stringified on the JS side the null will become a "null". So there needs to be a comparison against the string "null" and not the null value.

Bugs filed:

https://issues.apache.org/jira/browse/CB-1935

https://issues.apache.org/jira/browse/CB-1936


--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
 
To compile in the cloud, check out build.phonegap.com
 
 

Simon MacDonald

unread,
Nov 26, 2012, 11:55:36 AM11/26/12
to phonegap
Hey Raymond,

I fixed the docs but but I can't reproduce the code bug. Luckily the optJSONObject method correctly gives a null value instead of a null string. Do you have a repo scenario for me? I tried the code from your original email and the headers are specified correctly. What version of PG are you using?

colin mcdonald

unread,
Dec 20, 2012, 1:31:49 AM12/20/12
to phon...@googlegroups.com
Hey Raymond,

I could have sworn I had this working the other day (uploading a captured file from the camera) to the parse.com file server.  However, today, I was getting zero errors but the uploaded file seemed to be broken.

After many hours of tracking this down, (I'm using xcode/ios version of phonegap) I discovered that parse.com does not seem to handle mutlipart/formdata... and this is exactly how the FileTransfer iOS plugin works.  So, in fact, when I was sending the captured image through FileTransfer to parse.com file server, there was no error, but parse.com was taking the entire body of the HTTP Request (including all the multi part boundary lines) and saving that as one file and setting the content type as image/jpeg.  So, of course, when I went to the URL in my browser, the "image" was corrupted.

(Great, I just saw your question: https://parse.com/questions/file-upload-returns-201-but-doesnt-seem-to-be-valid  haha wish I had found this a few hours ago.  But, hey, I've learned so much!)

So, maybe this is a question to the group:  Should we have an option on the FileTransfer object to send the data not as mutlipart but simply the entire body of the request is the file (does this go against some standard)?  Or do I create a seperate plugin ParseFileTransfer that simply does
Reply all
Reply to author
Forward
0 new messages