Request and Response

Constructed Request and Response bodies, clone, and response.ok for 2xx only, verified in Node.js 22.23.3 without fetch or CORS.

模块页正文目前为英文。

Request and Response are the Fetch API body objects. You can construct them in memory without calling fetch. A body can be consumed once: text(), json(), or arrayBuffer() sets bodyUsed, and a second read throws TypeError. clone() must happen before the body is consumed.

response.ok is true only when status is in 200–299. 404 and 500 are not ok. Examples were checked in Node.js 22.23.3. They never call fetch, so CORS, redirects, Set-Cookie jar behavior, and cache mode are not measured. Request objects in this runtime still expose default mode, credentials, and referrer property values; those fields were not exercised against a network.

Examples were checked in Node.js 22.23.3, which provides these Web APIs as built-in globals. This is not a browser DOM test, and the examples do not send network requests. See the MDN documentation for the complete API, including browser support.

Web APIs overview → · JavaScript tutorial and built-in APIs →

API index

APISummary
Request.text(), bodyUsed, clone()Read a Request body once, clone it before it is used, and reject a GET request that includes a body.
new Response(), response.ok, response.json(), response.text()Construct a Response, treat ok as a 2xx test only, and consume the body once.

One-shot bodies and clone

new Request("https://example.com/api", { method: "POST", body: "hello" }) starts with bodyUsed false. await request.text() returns "hello" and then bodyUsed is true. A second text() throws TypeError.

clone() taken before the first read yields two independent bodies. clone() after the body is used throws TypeError. new Request(url, { method: "GET", body: "x" }) also throws TypeError, because GET and HEAD cannot have a body. The URL is only constructed; nothing is fetched.

Request.text(), bodyUsed, clone()

new Request(input, { method, headers, body, signal })
Request.prototype.text()
Request.prototype.clone()
request.bodyUsed

Read a Request body once, clone it before it is used, and reject a GET request that includes a body.

ParameterDescription
inputA URL string or another Request. The example uses https://example.com/api and never fetches it.
bodyThe request body. It can be consumed once unless you clone first.

Returns: text() returns a promise for the body string. clone() returns a new Request with an unread copy of the body.

Throws

  • TypeError — Thrown when the body is read or cloned after it is used, and when a GET request is constructed with a body. Measured in Node.js 22.23.3.
  • Passing a signal into Request is not demonstrated with fetch. An already aborted signal does not, by itself, contact a server.
javascript
const request = new Request("https://example.com/api", {
  method: "POST",
  headers: { "Content-Type": "text/plain" },
  body: "hello",
});
console.log(request.method, request.url, request.bodyUsed);
console.log(await request.text());
console.log(request.bodyUsed);
try {
  await request.text();
} catch (error) {
  console.log(error.name);
}

const original = new Request("https://example.com/api", { method: "POST", body: "hello" });
const clone = original.clone();
console.log(await original.text(), await clone.text());

const used = new Request("https://example.com/api", { method: "POST", body: "hello" });
await used.text();
try {
  used.clone();
} catch (error) {
  console.log(error.name);
}

try {
  new Request("https://example.com/", { method: "GET", body: "x" });
} catch (error) {
  console.log(error.name);
}

Expected output

POST https://example.com/api false
hello
true
TypeError
hello hello
TypeError
TypeError

Related: Headers, AbortController

MDN documentation →

status, ok, and a consumed Response body

new Response(body, { status, statusText, headers }) builds a response in memory. A 200 response has ok true. type is "default" and url is an empty string for this constructed response in Node.js 22.23.3. json() parses the body once.

ok is true for 201 and 299, and false for 300, 404, and 500. It is not a generic "the call worked" flag; it is only a 2xx check. A status outside 200–599, such as 199, throws RangeError in this runtime. A Response body is also one-shot: a second text() throws TypeError.

new Response(), response.ok, response.json(), response.text()

new Response(body, { status, statusText, headers })
response.ok
response.status
Response.prototype.json()
Response.prototype.text()

Construct a Response, treat ok as a 2xx test only, and consume the body once.

ParameterDescription
statusAn HTTP status. In Node.js 22.23.3 it must be in the inclusive range 200 to 599, or the constructor throws RangeError.
bodyThe response body, or null. Like Request, it can be read only once.

Returns: A Response. ok is a boolean: true only for status values from 200 to 299 inclusive, as measured here.

Throws

  • RangeError — Thrown by the constructor when status is 199 in Node.js 22.23.3, with a message that status must be in the range 200 to 599.
  • TypeError — Thrown when the body is read a second time.
  • fetch(), CORS, redirects, and cookie handling are not demonstrated. Do not treat these constructed objects as a measurement of a live HTTP exchange.
javascript
const ok = new Response('{"ok":true}', {
  status: 200,
  statusText: "OK",
  headers: { "Content-Type": "application/json" },
});
console.log(ok.status, ok.ok, ok.statusText, ok.type, JSON.stringify(ok.url));
console.log(JSON.stringify(await ok.json()));

const missing = new Response("missing", { status: 404, statusText: "Not Found" });
console.log(missing.status, missing.ok, missing.statusText);

console.log(new Response(null, { status: 201 }).ok);
console.log(new Response(null, { status: 299 }).ok);
console.log(new Response(null, { status: 300 }).ok);
console.log(new Response("err", { status: 500 }).ok);

try {
  new Response(null, { status: 199 });
} catch (error) {
  console.log(error.name);
}

const body = new Response("hello");
console.log(body.bodyUsed);
console.log(await body.text());
try {
  await body.text();
} catch (error) {
  console.log(error.name);
}

Expected output

200 true OK default ""
{"ok":true}
404 false Not Found
true
true
false
false
RangeError
false
hello
TypeError

MDN documentation →