JavaScript: Response
A Response object contains the status and body returned by fetch().
What you will learn
- How to check
response.okandresponse.status - How to read a body with
response.json()orresponse.text() - Why a body can be consumed only once and how to handle failures
Minimal example
fetch("data.json")
.then(response => {
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response.json();
})
.then(data => {
console.log("Data received:", data);
})
.catch(error => {
console.error("Could not load data:", error);
});The first callback receives the Response object. Check the status before reading the body, then return the Promise from json() or text() so the next step receives the converted value.
Useful properties and methods
response.okistruefor successful HTTP statuses.response.statuscontains the numeric status, such as 200 or 404.response.json()parses a JSON body.response.text()reads a text body.
Reading a body consumes it. Choose the method that matches the format you need, and do not call both on the same response unless you clone it first.