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

Fix multipart/form-data being handled as json #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

einkoro
Copy link

@einkoro einkoro commented May 1, 2021

When working with web workers in the browser (or CloudFlare Workers) we do not have access to FormData.getHeaders() to construct requests (example in #121 (comment)) and bent will default to application/json whereas fetch should handle the content type and boundary headers when passing an instance of FormData to it.

This pull request changes bent to skip adding the application/json content-type header and stringifying the body when the body is an instanceof FormData.

Do not add application/json content-type header or stringify the body when the body is an instanceof FormData.
@Zoramite
Copy link

I've tested this fix locally and it works great. It allows you to send form data using native FormData in the browser without needing an external library.

For example:

const postJSON = bent('json', 'POST');

const formData = new FormData();
// File data from form field, drag and drop, etc.
formData.append('file', fileData);

const response = await postJSON('https://...', formData);

Since the content-type doesn't get set the browser is correctly able to use the FormData object to send the form as a multi-part request in the fetch.

@bill-work-acc
Copy link

For anyone who can't wait this PR to be merged, can use npm to install this PR version of bent directly

npm i mikeal/bent#pull/135/head

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