Request: formData() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Note: This feature is available in Web Workers.

The formData() method of the Request interface reads the request body and returns it as a promise that resolves with a FormData object.

Syntax

js
formData()

Parameters

None.

Return value

A Promise that resolves with a FormData object.

Exceptions

TypeError

Thrown for one of the following reasons:

  • The request body is disturbed or locked.
  • There was an error decoding the body content (for example, because the Content-Encoding header is incorrect).
  • The MIME type of the body cannot be determined from the Content-Type headers included in the request, or is not application/x-www-form-urlencoded or multipart/form-data.
  • The body cannot be parsed as a FormData object.

Examples

js
const formData = new FormData();
const fileField = document.querySelector('input[type="file"]');

formData.append("username", "abc123");
formData.append("avatar", fileField.files[0]);

const request = new Request("/myEndpoint", {
  method: "POST",
  body: formData,
});

request.formData().then((data) => {
  // do something with the formdata sent in the request
});

Specifications

Specification
Fetch
# ref-for-dom-body-formdata①

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
formData
Chrome – Partial support
Chrome 60 (Release date: 2017-07-25)
footnote Partial support
footnote If the form data is empty, then Chrome throws TypeError: Failed to fetch. See bug 40065051 and bug 40764130.
Edge – Partial support
Edge 79 (Release date: 2020-01-15)
footnote Partial support
footnote If the form data is empty, then Edge throws TypeError: Failed to fetch. See bug 40065051 and bug 40764130.
Firefox – Full support
Firefox 39 (Release date: 2015-07-02)
footnote Full support
Opera – Partial support
Opera 47 (Release date: 2017-08-09)
footnote Partial support
footnote If the form data is empty, then Opera throws TypeError: Failed to fetch. See bug 40065051 and bug 40764130.
Safari – Partial support
Safari 11.1 – 14 (Release date: 2018-04-12)
footnote Partial support
footnote The method exists but always rejects with NotSupportedError. See bug 215671.
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote Full support
Chrome Android – Partial support
Chrome Android 60 (Release date: 2017-08-01)
footnote Partial support
footnote If the form data is empty, then Chrome Android throws TypeError: Failed to fetch. See bug 40065051 and bug 40764130.
Firefox for Android – Full support
Firefox for Android 39 (Release date: 2015-07-02)
footnote Full support
Opera Android – Partial support
Opera Android 44 (Release date: 2017-12-11)
footnote Partial support
footnote If the form data is empty, then Opera Android throws TypeError: Failed to fetch. See bug 40065051 and bug 40764130.
Safari on iOS – Partial support
Safari on iOS 11.3 – 14 (Release date: 2018-03-29)
footnote Partial support
footnote The method exists but always rejects with NotSupportedError. See bug 215671.
Safari on iOS – Full support
Safari on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Samsung Internet – Partial support
Samsung Internet 8 (Release date: 2018-07-18)
footnote Partial support
footnote If the form data is empty, then Samsung Internet throws TypeError: Failed to fetch. See bug 40065051 and bug 40764130.
WebView Android – Partial support
WebView Android 60 (Release date: 2017-08-01)
footnote Partial support
footnote If the form data is empty, then WebView Android throws TypeError: Failed to fetch. See bug 40065051 and bug 40764130.
WebView on iOS – Partial support
WebView on iOS 11.3 – 14 (Release date: 2018-03-29)
footnote Partial support
footnote The method exists but always rejects with NotSupportedError. See bug 215671.
WebView on iOS – Full support
WebView on iOS 14.5 (Release date: 2021-04-26)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 18 (Release date: 2022-04-19)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
Has more compatibility info.

See also