Response: formData() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Want more browser support for this feature? Tell us why.
Note: This feature is available in Web Workers.
The formData() method of the Response interface
takes a Response stream and reads it to completion. It returns a promise
that resolves with a FormData object.
Note:
This is mainly relevant to service workers. If a user submits
a form and a service worker intercepts the request, you could for example call
formData() on it to obtain a key-value map, modify some fields, then send
the form onwards to the server (or use it locally).
Syntax
js
formData()
Parameters
None.
Return value
Exceptions
AbortErrorDOMException-
The request was aborted.
TypeError-
Thrown for one of the following reasons:
- The response body is disturbed or locked.
- There was an error decoding the body content (for example, because the
Content-Encodingheader is incorrect). - The MIME type of the body cannot be determined from the
Content-Typeheaders included in the response, or is notapplication/x-www-form-urlencodedormultipart/form-data. - The body cannot be parsed as a
FormDataobject.
Examples
>Using formData()
js
const response = new Response("name=Alex&city=London", {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
const formData = await response.formData();
console.log(formData.get("name")); // "Alex"
Specifications
| Specification |
|---|
| Fetch> # ref-for-dom-body-formdata①> |