Request.text(), bodyUsed, clone()
new Request(input, { method, headers, body, signal })
Request.prototype.text()
Request.prototype.clone()
request.bodyUsedRead a Request body once, clone it before it is used, and reject a GET request that includes a body.
| Parameter | Description |
|---|---|
input | A URL string or another Request. The example uses https://example.com/api and never fetches it. |
body | The 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.
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