APIs Reference

When Is Response.ok True?

Response.ok is true exactly when the HTTP status is 200 through 299, and false for every other status. It does not validate the body, content type, or application-level success, and opaque responses report ok as false.

The short answer

Response.ok is a read-only boolean that is true exactly when the response's HTTP status code is in the range 200 through 299 inclusive. Every other status, including redirects, 304 Not Modified, 4xx client errors, and 5xx server errors, produces false.

The flag answers one narrow question: did the server return a success-class status? It does not say that the body is well-formed, that the content type matches what you expected, or that the application-level operation succeeded. A 200 response carrying {"error": "quota exceeded"} has ok === true.

A minimal example

async function getJson(url) {
  const response = await fetch(url, {
    headers: { Accept: "application/json" },
  });

  if (!response.ok) {
    throw new Error(`HTTP ${response.status} ${response.statusText} for ${url}`);
  }

  const type = response.headers.get("content-type") ?? "";
  if (!type.includes("application/json")) {
    throw new Error(`Expected JSON, got ${type || "no content type"}`);
  }

  return response.json();
}

The status check comes first because fetch() does not reject on HTTP errors. A 404 or 500 still resolves the promise; only network-level failures reject it. The content-type check matters because response.json() also rejects on a syntax error, and many servers return an HTML error page with status 200.

Which statuses set ok

Status ok Meaning
200 true success
201 true created
204 true success, no body
206 true partial content
299 true upper boundary
300 false multiple choices
304 false not modified
404 false not found
500 false server error

The boundaries are easy to test: 299 is true, 300 is false. Statuses in the 1xx class are not final responses and are not exposed to script through fetch(), so they do not normally matter here.

A 304 matters when you reason about cache revalidation: it is a successful revalidation for the cache but not a 2xx status for ok. See HTTP 304 responses have no body for how that response is handled.

What changes the answer

Redirects. By default fetch() follows redirects and ok describes the final response. If the redirect target returns 200, ok is true even though the original URL returned a 301 or 302. Check response.redirected and response.url if the distinction matters.

Response type. With mode: "no-cors", the browser returns an opaque response whose status is 0 and whose ok is false, regardless of what the server actually sent. The same status-0 shape appears for redirect: "manual" in browsers. In those cases ok carries no information about the server's status.

Runtime. Node.js and other non-browser runtimes implement the same 200-299 rule for ordinary responses, but they do not have CORS-opaque responses, so status 0 is rare there.

The method. A HEAD request or a 204 or 205 response has a null body. ok can be true while response.json() rejects on that empty body; response.text() resolves to an empty string instead. A successful status does not guarantee that the body matches the media type you expected.

Practical checks

  • Do not rely on response.ok alone for APIs that return 200 with an error document; validate the payload shape as well.
  • Log status, ok, type, redirected, and content-type together when debugging a fetch failure.
  • If you need to distinguish status classes, test response.status directly rather than deriving meaning from ok.
  • Remember that a rejected fetch() promise means no HTTP response was received at all; an ok === false response means one was received and it was not 2xx.

Sources

Revision history

Published
Updated
— Initial publication.