class: Route

class: Route

Whenever a network route is set up with page.route(url, handler) or browserContext.route(url, handler), the Route object allows to handle the route.

  • route.abort([errorCode])
  • route.continue([overrides])
  • route.fulfill(response)
  • route.request()

route.abort([errorCode])

  • errorCode <string “String”)> Optional error code. Defaults to failed, could be one of the following:
    • 'aborted' - An operation was aborted (due to user action)
    • 'accessdenied' - Permission to access a resource, other than the network, was denied
    • 'addressunreachable' - The IP address is unreachable. This usually means that there is no route to the specified host or network.
    • 'blockedbyclient' - The client chose to block the request.
    • 'blockedbyresponse' - The request failed because the response was delivered along with requirements which are not met (‘X-Frame-Options’ and ’Content-Security-Policy‘ ancestor checks, for instance).
    • 'connectionaborted' - A connection timed out as a result of not receiving an ACK for data sent.
    • 'connectionclosed' - A connection was closed (corresponding to a TCP FIN).
    • 'connectionfailed' - A connection attempt failed.
    • 'connectionrefused' - A connection attempt was refused.
    • 'connectionreset' - A connection was reset (corresponding to a TCP RST).
    • 'internetdisconnected' - The Internet connection has been lost.
    • 'namenotresolved' - The host name could not be resolved.
    • 'timedout' - An operation timed out.
    • 'failed' - A generic failure occurred.
  • returns: <Promise “Promise”)>

Aborts the route‘s request.

route.continue([overrides])

  • overrides <Object “Object”)> Optional request overrides, which can be one of the following:
    • method <string “String”)> If set changes the request method (e.g. GET or POST)
    • postData <string “String”)> If set changes the post data of request
    • headers <Object “Object”)<string ”String“), string “String”)>> If set changes the request HTTP headers. Header values will be converted to a string.
  • returns: <Promise “Promise”)>

Continues route‘s request with optional overrides.

await page.route('**/*', (route, request) => {
  // Override headers
  const headers = Object.assign({}, request.headers(), {
    foo: 'bar', // set "foo" header
    origin: undefined, // remove "origin" header
  });
  route.continue({headers});
});

route.fulfill(response)

  • response <Object “Object”)> Response that will fulfill this route‘s request.
    • status <number “Number”)> Response status code, defaults to 200.
    • headers <Object “Object”)<string ”String“), string “String”)>> Optional response headers. Header values will be converted to a string.
    • contentType <string “String”)> If set, equals to setting Content-Type response header.
    • body <string “String”)|Buffer ”Buffer“)> Optional response body.
    • path <string “String”)> Optional file path to respond with. The content type will be inferred from file extension. If path is a relative path, then it is resolved relative to current working directory.
  • returns: <Promise “Promise”)>

Fulfills route‘s request with given response.

An example of fulfilling all requests with 404 responses:

await page.route('**/*', route => {
  route.fulfill({
    status: 404,
    contentType: 'text/plain',
    body: 'Not Found!'
  });
});

An example of serving static file:

await page.route('**/xhr_endpoint', route => route.fulfill({ path: 'mock_data.json' }));

route.request()

  • returns: <Request> A request to be routed.