Version: 1.4.0

Request

Whenever the page sends a request for a network resource the following sequence of events are emitted by Page:

  • 'request' emitted when the request is issued by the page.
  • 'response' emitted when/if the response status and headers are received for the request.
  • 'requestfinished' emitted when the response body is downloaded and the request is complete.

If request fails at some point, then instead of 'requestfinished' event (and possibly instead of 'response' event), the 'requestfailed' event is emitted.

NOTE HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will complete with 'requestfinished' event.

If request gets a 'redirect' response, the request is successfully finished with the 'requestfinished' event, and a new request is issued to a redirected url.

request.failure()#

  • returns: <null|Object> Object describing request failure, if any
    • errorText <string> Human-readable error message, e.g. 'net::ERR_FAILED'.

The method returns null unless this request has failed, as reported by requestfailed event.

Example of logging of all the failed requests:

page.on('requestfailed', request => {
console.log(request.url() + ' ' + request.failure().errorText);
});

request.frame()#

  • returns: <Frame> A Frame that initiated this request.

request.headers()#

  • returns: <Object<string, string>> An object with HTTP headers associated with the request. All header names are lower-case.

request.isNavigationRequest()#

Whether this request is driving frame's navigation.

request.method()#

  • returns: <string> Request's method (GET, POST, etc.)

request.postData()#

  • returns: <null|string> Request's post body, if any.

request.postDataBuffer()#

  • returns: <null|Buffer> Request's post body in a binary form, if any.

request.postDataJSON()#

  • returns: <null|Object> Parsed request's body for form-urlencoded and JSON as a fallback if any.

When the response is application/x-www-form-urlencoded then a key/value object of the values will be returned. Otherwise it will be parsed as JSON.

request.redirectedFrom()#

  • returns: <null|Request> Request that was redirected by the server to this one, if any.

When the server responds with a redirect, Playwright creates a new Request object. The two requests are connected by redirectedFrom() and redirectedTo() methods. When multiple server redirects has happened, it is possible to construct the whole redirect chain by repeatedly calling redirectedFrom().

For example, if the website http://example.com redirects to https://example.com:

const response = await page.goto('http://example.com');
console.log(response.request().redirectedFrom().url()); // 'http://example.com'

If the website https://google.com has no redirects:

const response = await page.goto('https://google.com');
console.log(response.request().redirectedFrom()); // null

request.redirectedTo()#

  • returns: <null|Request> New request issued by the browser if the server responded with redirect.

This method is the opposite of request.redirectedFrom():

console.log(request.redirectedFrom().redirectedTo() === request); // true

request.resourceType()#

Contains the request's resource type as it was perceived by the rendering engine. ResourceType will be one of the following: document, stylesheet, image, media, font, script, texttrack, xhr, fetch, eventsource, websocket, manifest, other.

request.response()#

request.url()#

  • returns: <string> URL of the request.