The Response object
Whenfetch 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
Checking response.ok
The most important property. It’strue for any 2xx status code:
Extracting the response body
The Response object has methods to read the body in different formats:.json() — the one you’ll use 90% of the time
.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: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