Skip to content

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

Open
einkoro wants to merge 1 commit into
mikeal:masterfrom
einkoro:multipart/form-data
Open

Fix multipart/form-data being handled as json#135
einkoro wants to merge 1 commit into
mikeal:masterfrom
einkoro:multipart/form-data

Conversation

@einkoro

@einkoro einkoro commented May 1, 2021

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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.

3 participants