Skip to content

Add async Content-Length computation when using form-data - #953

Merged
mikeal merged 1 commit into
request:masterfrom
LoicMahieu:master
Jul 9, 2014
Merged

mikeal merged 1 commit into
request:masterfrom
LoicMahieu:master

Conversation

@LoicMahieu

Copy link
Copy Markdown
Member

Related to #345 #396 #522

@mikeal

mikeal commented Jul 2, 2014

Copy link
Copy Markdown
Member

how does form.getLength get the length of the form? if it's buffering the entire form in to memory then we can't really take this as a default.

@LoicMahieu

Copy link
Copy Markdown
Member Author

getLength()

We can see here: _lengthRetrievers how node-form-data computes the length. I don't think that the data is loaded in memory.

@mikeal

mikeal commented Jul 2, 2014

Copy link
Copy Markdown
Member

ok, the code is pretty obfuscated but it looks like it has some built in length retrievers for each stream that gets added to it. when those streams are known it can compute the length (if it has a .path property it'll fs.stat the path).

I believe it errors when it doesn't understand the stream. In that case, you'll need to modify this patch so that when getLength's callback gets an error you just opt out of setting the length rather than actually emit an error in the stream.

@LoicMahieu

Copy link
Copy Markdown
Member Author

I updated the PR.

(I updated my comment clarify. The problem was obviously not caused by async)

There is a problem when the stream is created before the execution of the request.
Here is an example that explains the problem:

function doRequest (stream) {
  var r = request.post('http://requestb.in/1baaynt1', function (err, httpResponse, body) {
    if (err) {
      throw err;
    }
    console.log(httpResponse, body);
  });
  var form = r.form();
  form.append('file', stream);
}

// works
var stream = fs.createReadStream('test.jpg');
doRequest(stream);

// not works
var stream = fs.createReadStream('test.jpg');
setTimeout(function () {
  doRequest(stream);
}, 100);

I don't really know what happens but I continue to investigate... Any idea?

(In my case, I createReadStream before passing into multiple async.waterfall.)

@LoicMahieu

Copy link
Copy Markdown
Member Author

@mikeal Any thought ?

@mikeal

mikeal commented Jul 7, 2014

Copy link
Copy Markdown
Member

what version of node?

@LoicMahieu

Copy link
Copy Markdown
Member Author

Ok. Works great with v0.11.13 but not with v0.10.29.
Do you know what has changed in node stream behavior ?

@LoicMahieu

Copy link
Copy Markdown
Member Author

Rebase done. Travis build in error caused by #951

@mikeal

mikeal commented Jul 9, 2014

Copy link
Copy Markdown
Member

Sorry this one took so long, I was busy with NodeConf :)

mikeal added a commit that referenced this pull request Jul 9, 2014
Add async Content-Length computation when using form-data
@mikeal
mikeal merged commit 1d7ae27 into request:master Jul 9, 2014
@LoicMahieu

Copy link
Copy Markdown
Member Author

Thanks for merging and your trust :)

@reverentgeek

Copy link
Copy Markdown

FYI... I ran into this same issue. We use Akamai. File uploads work fine locally, but not in production. Using the fix in this pull request resolved the issue. When will this get published?

Thanks!

nylen pushed a commit to nylen/request that referenced this pull request Oct 17, 2014
Add async Content-Length computation when using form-data
sabaoongfx pushed a commit to sabaoongfx/request-modern that referenced this pull request Mar 10, 2026
Add async Content-Length computation when using form-data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants