Course contentsShow
← JavaScript
Lesson 2287 of 2,72347. Networking PrimitivesFree lesson

Reading Response Bodies

Using response.json(), response.text(), response.blob(), and response.arrayBuffer() to consume data.

Reading Response Bodies

You'll learn: How to extract and convert the data returned by a fetch() request using the four main body-reading methods.

The Core Idea

When you receive a Response object from fetch(), the actual data isn't immediately accessible as a JavaScript value. The body is a stream that needs to be read and converted into a usable format. Think of it like receiving a sealed envelope—you need to open it and decode the contents based on what format was used.

The Response object provides four key methods for this:

The Four Body-Reading Methods

response.json() — Parses the body as JSON and returns a Promise that resolves to a JavaScript object or array. Use this for API responses that return structured data.

response.text() — Reads the body as plain text and returns a Promise with a string. Perfect for HTML, plain text files, or CSV data.

response.blob() — Converts the body into a Blob object, useful for binary data like images, videos, or downloadable files.

response.arrayBuffer() — Returns the raw binary data as an ArrayBuffer, giving you byte-level access for custom processing or working with binary protocols.

Important Details

Each method returns a Promise because reading the stream is asynchronous. You can only read the body once—after calling any of these methods, the stream is consumed and subsequent calls will fail.

Choose your method based on the data format you expect from the server. Most modern APIs return JSON, making response.json() the most common choice.

Key Takeaway: The Response body must be explicitly read using response.json(), response.text(), response.blob(), or response.arrayBuffer() depending on the data format you need, and each method returns a Promise that resolves to the converted data.