Skip to main content
Version: 1.17

Route

Whenever a network route is set up with Page.route(url, handler[, options]) or BrowserContext.route(url, handler[, options]), the Route object allows to handle the route.

Route.abort([errorCode])

  • errorCode <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: <void>#

Aborts the route's request.

Route.fulfill([options])

  • options <Route.FulfillOptions>
    • setBody <String> Optional response body as text.#
    • setBodyBytes <byte[]> Optional response body as raw bytes.#
    • setContentType <String> If set, equals to setting Content-Type response header.#
    • setHeaders <Map<String, String>> Response headers. Header values will be converted to a string.#
    • setPath <Path> 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 the current working directory.#
    • setStatus <int> Response status code, defaults to 200.#
  • returns: <void>#

Fulfills route's request with given response.

An example of fulfilling all requests with 404 responses:

page.route("**/*", route -> {
route.fulfill(new Route.FulfillOptions()
.setStatus(404)
.setContentType("text/plain")
.setBody("Not Found!"));
});

An example of serving static file:

page.route("**/xhr_endpoint", route -> route.fulfill(
new Route.FulfillOptions().setPath(Paths.get("mock_data.json"))));

Route.request()

A request to be routed.

Route.resume([options])

  • options <Route.ResumeOptions>
    • setHeaders <Map<String, String>> If set changes the request HTTP headers. Header values will be converted to a string.#
    • setMethod <String> If set changes the request method (e.g. GET or POST)#
    • setPostData <String|byte[]> If set changes the post data of request#
    • setUrl <String> If set changes the request URL. New URL must have same protocol as original one.#
  • returns: <void>#

Continues route's request with optional overrides.

page.route("**/*", route -> {
// Override headers
Map<String, String> headers = new HashMap<>(route.request().headers());
headers.put("foo", "bar"); // set "foo" header
headers.remove("origin"); // remove "origin" header
route.resume(new Route.ResumeOptions().setHeaders(headers));
});