Skip to content

Add content-length header with form-data#58

Merged
bitinn merged 1 commit intonode-fetch:masterfrom
item4:master
Mar 9, 2016
Merged

Add content-length header with form-data#58
bitinn merged 1 commit intonode-fetch:masterfrom
item4:master

Conversation

@item4
Copy link
Contributor

@item4 item4 commented Nov 25, 2015

Old source do not add content-length header automatically
with form-data. If body is instance of form-data, Users
must missing this header. It cause parse error with some
server engine like WSGI. WSGI must drop body if this was not
set, and WSGI based frameworks such as flask can not read
form data.

For example, this python code with flask..

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/', method=['POST'])
def index():
    return jsonify(**request.form)

app.run()

Can not receive any data from this code.

require('es6-promise').polyfill();
var fetch = require('isomorphic-fetch'); // it use node-fetch
var FormData = require('form-data');

var data = new FormData();
data.append('name', 'item4');
data.append('message', 'it is test!');
fetch('http://localhost:5000', {method: 'POST', body: data})
  .then(function (data) {
    return data.json();
  })
  .then(function (data) {
    console.log(data);
  });

This commit just use FormData's getLengthSync method.

Old source do not add content-length header automatically
with form-data. If body is instance of form-data, Users
must missing this header. It cause parse error with some
server engine like WSGI. WSGI must drop body if this was not
set, and WSGI based frameworks such as flask can not read
form data.

For example, this python code with flask..

----
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/', method=['POST'])
def index():
    return jsonify(**request.form)

app.run()
----

Can not receive any data from this code.

----
require('es6-promise').polyfill();
var fetch = require('isomorphic-fetch'); // it use node-fetch
var FormData = require('form-data');

var data = new FormData();
data.append('name', 'item4');
data.append('message', 'it is test!');
fetch('http://localhost:5000', {method: 'POST', body: data})
  .then(function (data) {
    return data.json();
  })
  .then(function (data) {
    console.log(data);
  });
----

This commit just use FormData's getLengthSync method.
@bitinn
Copy link
Collaborator

bitinn commented Nov 26, 2015

I want to double check one thing, if you do manually pass headers: form.getHeaders() to fetch, it works right? Does form-data set content-length by itself?

@item4
Copy link
Contributor Author

item4 commented Nov 26, 2015

So sadly, No.

See this implementation.

@olalonde
Copy link

olalonde commented Mar 8, 2016

Having same issue, fetch doesn't set Content-Length header.

@bitinn
Copy link
Collaborator

bitinn commented Mar 9, 2016

@olalonde yep I should merge this...

bitinn added a commit that referenced this pull request Mar 9, 2016
Add content-length header with form-data
@bitinn bitinn merged commit 6be7c96 into node-fetch:master Mar 9, 2016
@bitinn
Copy link
Collaborator

bitinn commented Mar 9, 2016

And in case people need an alternative for passing custom headers with form-data, getCustomHeaders seems like a good solution.

https://github.com/form-data/form-data/blob/master/lib/form_data.js#L272

@dbo
Copy link

dbo commented Mar 11, 2016

@bitinn Any chance to get a new official release npm? The last one is 5 months old and 3 PRs have been landing meanwhile on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants