ScreenshotNeo

BlogHow-to

How to Get the Status Text from a Puppeteer Response

Read Puppeteer’s HTTP status phrase with `response.statusText()`. Learn how it differs from the status code and success flag, and handle missing responses and errors.

By the ScreenshotNeo team4 October 20265 min read

Call statusText() on Puppeteer’s HTTPResponse object:

const statusText = response.statusText();
console.log(statusText); // often "OK"

statusText() returns a string containing the response’s human-readable status phrase. For branching logic, use status() for the numeric code or ok() for a 2xx success check. See the Puppeteer statusText API reference and Page event reference.

Read status text from a navigation response

Navigation methods such as page.goto() return an HTTP response when navigation produces one. Check for null before reading it: some navigations, including a hash change to the same URL or navigation to about:blank, may have no response.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();
  const response = await page.goto('https://example.com', {
    waitUntil: 'domcontentloaded',
    timeout: 30_000,
  });

  if (response === null) {
    console.log('This navigation did not produce an HTTP response.');
  } else {
    console.log('Status text:', response.statusText());
    console.log('Status code:', response.status());
    console.log('Success:', response.ok());
  }
} finally {
  await browser.close();
}

Run this in an ES module with Puppeteer installed. The navigation wait condition controls when goto() resolves; it does not change how status text is read.

Read status text for every response

Use the page’s response event when you need responses for subresources as well as the main document. The callback receives an HTTPResponse:

page.on('response', response => {
  console.log(response.url(), response.status(), response.statusText());
});

A page can receive many responses: the document, scripts, stylesheets, images, API calls, and more. Filter by URL or resource type if you only want a particular response:

page.on('response', response => {
  if (response.request().resourceType() === 'document') {
    console.log('Document:', response.url(), response.statusText());
  }
});

Install the listener before navigating if you need to observe the initial document response. A response event reports that an HTTP response arrived; it does not mean the status is successful.

Status text, status code, and success

Value Puppeteer method Type Use it for
Status phrase statusText() String Logging or display, such as a phrase commonly reported as OK.
Status code status() Number Exact HTTP status checks, such as 404 or 503.
Success predicate ok() Boolean Checking whether the status is in the 200–299 range.

Do not make program behavior depend on a particular status phrase. The phrase is human-readable and can vary; the numeric status or ok() is the better fit for conditions.

if (response && !response.ok()) {
  console.error(`HTTP ${response.status()} ${response.statusText()}`);
}

HTTP errors are different from request failures

An HTTP error response such as 404 or 503 is still a response. Puppeteer’s request-failure event is for failures such as a timeout; an HTTP error status alone does not mean that the request failed at the network level. Inspect status() or ok() to detect HTTP error codes.

If you need to log both cases, handle them separately:

page.on('response', response => {
  if (!response.ok()) {
    console.error('HTTP response:', response.status(), response.statusText(), response.url());
  }
});

page.on('requestfailed', request => {
  console.error('Request failed:', request.url(), request.failure()?.errorText);
});

Complete example: report the main document response

This version captures the navigation response and reports the phrase, numeric code, and success state together. It also handles a missing response and makes browser cleanup run if navigation or logging throws.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();
  const response = await page.goto('https://example.com', {
    waitUntil: 'domcontentloaded',
    timeout: 30_000,
  });

  if (!response) {
    console.log('No HTTP response was produced by this navigation.');
  } else {
    const statusText = response.statusText();
    const status = response.status();
    const ok = response.ok();

    console.log({ url: response.url(), status, statusText, ok });

    if (!ok) {
      console.error(`The document returned HTTP ${status} ${statusText}`);
      process.exitCode = 1;
    }
  }
} finally {
  await browser.close();
}

Common errors and fixes

Symptom Cause Fix
Cannot read properties of null The navigation returned no HTTP response. Check the result of page.goto() before calling statusText().
The page is an error page, but no request failure was logged An HTTP response such as 404 is not itself a request failure. Check response.status() or response.ok() in the response handler.
The listener misses the first response The response event listener was registered after navigation began. Register page.on('response', ...) before calling goto().
The output is empty or unexpected The server’s status phrase may be empty or differ from an expected phrase. Use the numeric code for decisions; treat status text as display or diagnostic data.
Navigation timeout The page did not reach the selected waitUntil condition before the timeout. Choose a suitable wait condition and timeout for the page. A navigation timeout is not an HTTP status phrase; handle navigation errors separately.

Performance and reliability notes

  • Reading statusText() from an existing response is a direct accessor. The main cost and timing considerations are browser startup, navigation, and the chosen wait condition.
  • Use a response event for monitoring many resources, but filter it if you only need the main document to keep logs useful.
  • Keep HTTP status handling separate from navigation exceptions and request failures. That distinction makes retries and error reporting more reliable.
  • Do not retry solely because a status phrase is unfamiliar. Base retry policy on the numeric status, request failure, and the behavior your application needs.

Or skip the browser setup

If your task is to capture a page rather than inspect Puppeteer’s response object, ScreenshotNeo returns a screenshot or PDF from one request. See the API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers report the page verdict and billing status. An MCP server gives AI agents tools to take screenshots, get page information, and capture PDFs. The Free plan includes 1,000 shots a month with no card; paid plans start at $5 for 3,000 shots. Sign up for free and get 1,000 screenshots a month with no card.

FAQ

Does statusText() return OK for every successful response?

No. OK is a usual example, not a guarantee for every response. Use ok() or status() when correctness depends on the result.

Can I get a status phrase from a request that timed out?

A timeout may produce no HTTP response, so there may be no status phrase to read. Handle the request failure or navigation exception separately.

Does a 404 trigger Puppeteer’s requestfailed event?

No. A 404 is an HTTP response. Inspect its status code or success flag in the response event.