JavaScript Reference ยท Browser and Web APIs
The Request and Response objects
Reference for the Request and Response classes used by fetch(): constructors, properties, reading bodies once, clone(), Response.json() and service workers.
Request and Response are the two classes behind fetch(). A Request describes what to send (URL, method, headers, body); a Response describes what came back (status, headers, body). You usually get a Response from fetch() without ever constructing one, but you can create both yourself, which is useful for tests, caches and service workers.
Both share a body that is a one-shot stream: you can read it once with .json(), .text(), .arrayBuffer(), .blob(), .formData() or .bytes(). To read it twice, call .clone() first.
const response = new Response('{"ok":true}', { status: 200 }); const data = await response.json();
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
new Request(input, options) new Response(body, options) Response.json(data, options) // static helper Response.error() // network error response Response.redirect(url, status) // redirect response
Request constructor
| Name | Description |
|---|---|
input |
A URL string, a URL or another Request to copy. |
options |
Same fields as the fetch() options: method, headers, body, mode, credentials, cache, redirect, signal. |
Response constructor
| Name | Description |
|---|---|
body |
Optional: string, Blob, ArrayBuffer, FormData, URLSearchParams, stream or null. |
options.status |
Integer from 200 to 599 (default 200). |
options.statusText |
Text for the status line (default empty). |
options.headers |
Plain object, pairs or Headers. |
Shared body methods: json(), text(), blob(), arrayBuffer(), formData(), bytes() and clone(), plus the body stream and the bodyUsed flag.
Useful Response properties: ok (status 200-299), status, statusText, headers, url (final URL), redirected, type ("basic", "cors", "opaque", ...).
Useful Request properties: url, method, headers, mode, credentials, signal.
Exceptions: the constructors throw TypeError for invalid values, for example a status outside 200-599 or a body with GET. Reading a body that is already used rejects with a TypeError.
Usage notes
- One read per body.
bodyUsedbecomestrueafter the first read. Clone before you read if you need the data twice. response.okis a convenience for status 200-299. A404still resolves with a normalResponse.Response.json()sets the headercontent-type: application/jsonfor you.new Response(JSON.stringify(x))does not.- Opaque responses. A
no-corscross-origin fetch returns type"opaque"with status 0 and an unreadable body. - Headers on fetched responses are read-only, and some request headers (such as
CookieorHost) are forbidden for scripts to set. - Service workers and tests. Constructing a
Responseis the way to fake a server answer, as in the stand-in used on thefetch()page. - Large bodies. Use
response.body.getReader()orfor awaitover the stream to process data in chunks instead of loading it all.
Browser support
Request and Response are supported in every current browser and in Node.js. Response.json() and bytes() are newer than the rest, so check a compatibility table when you target older browsers.