Response bodies, parsing and one-time consumption
Quick answer
A Response body is a stream that normally can be consumed only once. Body readers such as json() and text() consume it; clone before reading when two consumers really need it.
How it works
bodyUsed indicates that the stream has been disturbed; a locked stream can also be unusable. json() parses bytes as JSON, not a validated application record. clone() creates a response sharing a tee of the original stream; buffering may grow if consumers read at different speeds. A successful response with malformed JSON rejects during parsing.
Local examples
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {const r=new Response("{\"n\":2}".replaceAll("\\",""));const c=r.clone();console.log((await r.json()).n,await c.text(),r.bodyUsed);})()Expected console output:
2 {"n":2} trueBoth copies are created before consumption.
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {const r=new Response("bad-json");try{await r.json();}catch(e){console.log(e.name,r.bodyUsed);}})()Expected console output:
SyntaxError trueReading consumes the stream even when JSON parsing fails.
Common mistakes
Do not call text() followed by json() on the same consumed body. For empty 204 responses, skip JSON decoding.
Guided practice
Read JSON from one clone and text from the other; explain why copying response metadata alone is not enough.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Response bodies, parsing and one-time consumption — Knowledge Check
Apply the concept and reason about the example output.
When should a response be cloned for two readers?
Does json() validate an application schema?
Why avoid cloning very large bodies unnecessarily?
What is the expected console output of Response bodies, parsing and one-time consumption example 1?
(async () => {const r=new Response("{\"n\":2}".replaceAll("\\",""));const c=r.clone();console.log((await r.json()).n,await c.text(),r.bodyUsed);})()What is the expected console output of Response bodies, parsing and one-time consumption example 2?
(async () => {const r=new Response("bad-json");try{await r.json();}catch(e){console.log(e.name,r.bodyUsed);}})()