Version: 1.3.0

Request

Whenever the page sends a request, such as for a network resource, the following events are emitted by playwright's page:

  • 'request' emitted when the request is issued by the page.
  • 'response' emitted when/if the response is 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: <?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: <?string> Request's post body, if any.

request.postDataBuffer()#

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

request.postDataJSON()#

  • returns: <?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: <?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: <?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.