Skip to main content

The Response object

When fetch completes, you get a Response object. It’s not the data itself — it’s a wrapper with metadata about the response and methods to extract the body.

Key response properties

Status codes you’ll see most often

You don’t need to memorize all status codes. The important ones: 200 (success), 201 (created), 204 (no content), 400 (bad request), 401 (unauthorized), 404 (not found), 500 (server error).

Checking response.ok

The most important property. It’s true for any 2xx status code:
fetch does not throw an error for 404 or 500 responses. It only throws on network failures (no internet, DNS error). You must check response.ok yourself. This is the most common source of bugs in fetch code.

Extracting the response body

The Response object has methods to read the body in different formats:
You can only read the body once. After calling .json(), you can’t call .text() on the same response. If you need the body in multiple formats, use .text() first and parse manually.

.json() — the one you’ll use 90% of the time

Remember: .json() returns a Promise, so you need await. It calls JSON.parse() internally.

Reading response headers

FastAPI and other backends can send custom headers like X-Total-Count for pagination. Access them with response.headers.get("Header-Name").

Handling different response shapes

Your FastAPI backend might return data in different structures. Handle them appropriately:

Handling error responses from your API

FastAPI returns structured error responses. Parse them to show useful messages:
Don’t just throw generic “request failed” errors. Parse the error response body — your FastAPI backend sends useful validation messages. Show those to your users.

Complete pattern

Here’s how all of this comes together in a real API function:

What’s next?

You know how to read responses. But what happens when things go wrong? Let’s learn how to handle errors gracefully.

Error handling

Handle errors in your API calls and async code