Request Body
You'll learn: What request.body is, how it works as a ReadableStream, and when you can actually access it.
What Is the Request Body?
When you create a Request object using the Fetch API, the body property holds the data you're sending to the server. This could be JSON data, form data, or any other content you need to transmit.
Here's the important part: request.body is a ReadableStream, not a simple string or object. This means the data flows in chunks rather than being immediately available all at once.
Understanding ReadableStream
Think of request.body like water flowing through a pipe rather than water already sitting in a bucket. You can't just grab it all instantly—you need to collect it as it flows.
This streaming approach is powerful because it lets you handle large amounts of data efficiently without loading everything into memory at once.
When Is request.body Available?
The body property exists on Request objects you create, but there's a catch: once you read the body (using .json(), .text(), or similar methods), the stream is consumed and you can't read it again.
You'll typically interact with request.body when:
- Building custom request handling logic
- Working with Service Workers that intercept requests
- Cloning requests before consuming the body
In browser-side JavaScript using basic Fetch, you usually set the body during creation and don't read it back—the server receives and reads it instead.
Key Takeaway: request.body is a ReadableStream containing the data being sent to the server; it can only be consumed once and is primarily useful in advanced scenarios like Service Workers or request inspection.