Add content-length header with form-data#58
Merged
bitinn merged 1 commit intonode-fetch:masterfrom Mar 9, 2016
item4:master
Merged
Add content-length header with form-data#58bitinn merged 1 commit intonode-fetch:masterfrom item4:master
bitinn merged 1 commit intonode-fetch:masterfrom
item4:master
Conversation
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.
Collaborator
|
I want to double check one thing, if you do manually pass |
Contributor
Author
|
So sadly, No. |
|
Having same issue, fetch doesn't set Content-Length header. |
Collaborator
|
@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
Collaborator
|
And in case people need an alternative for passing custom headers with form-data, https://github.com/form-data/form-data/blob/master/lib/form_data.js#L272 |
|
@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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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..
Can not receive any data from this code.
This commit just use FormData's getLengthSync method.