Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect formdata body and set content-type header. #220

Merged
merged 1 commit into from
Sep 4, 2016

Conversation

DylanPiercey
Copy link
Contributor

This PR adds a check for Form-Data which makes it easier for users to submit form data bodies (no longer have to manually get headers from form-data).

This is similar to node-fetch/node-fetch#31, the idea is just to make it easier on end users since Form-Data is commonly used throughout the community. It will also make isomorphic code easier in the future with isomorphic-form-data.

I realize this is missing a new test, I wasn't sure what the best way to go about it was.
If I got some direction I could go ahead with tests or someone else could take over from here.

if (isPlainObj(body)) {
if (isStream(body) && typeof body.getBoundary === 'function') {
// Special case for https://github.com/form-data/form-data
opts.headers['content-type'] = opts.headers['content-type'] || 'multipart/form-data; boundary=' + body.getBoundary();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

content-type is not the only header, that can be returned from getHeaders function. This line should contain call to getHeaders and assign user supplied headers over it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`multipart/form-data; boundary=${body.getBoundary()}`;

@sindresorhus
Copy link
Owner

The test should send a request and check that correct headers were actually sent. See other tests for how to do it.

@sindresorhus
Copy link
Owner

ping @DylanPiercey :)

@DylanPiercey
Copy link
Contributor Author

@sindresorhus sorry forgot about this one, I'll try to get to it later today.

@floatdrop currently formdata getHeaders only returns content-type and that will not change so I figured this was the most appropriate api. Especially since in @2.0 the getHeaders function may have a deprecation warning.

@DylanPiercey
Copy link
Contributor Author

@sindresorhus does that look better? (Added some tests).

@DylanPiercey
Copy link
Contributor Author

Let me know if anything else is needed @sindresorhus 😀.

@sindresorhus
Copy link
Owner

sindresorhus commented Sep 4, 2016

LGTM when #220 (comment) is fixed. @floatdrop ?

@floatdrop
Copy link
Contributor

Especially since in @2.0 the getHeaders function may have a deprecation warning.

Okay then.

LGTM when #220 (comment) is fixed. @floatdrop ?

Lgtm. Waiting for #220 (comment)

@sindresorhus sindresorhus merged commit 44ce168 into sindresorhus:master Sep 4, 2016
@sindresorhus
Copy link
Owner

Actually, I'll just fix it when merging. Thanks @DylanPiercey :)

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.

None yet

3 participants