Skip to main content
Version: 1.22

Route

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

Learn more about networking.

route.abort(**kwargs)

  • error_code <str> 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: <NoneType>#

Aborts the route's request.

route.continue_(**kwargs)

  • headers <Dict[str, str]> If set changes the request HTTP headers. Header values will be converted to a string.#
  • method <str> If set changes the request method (e.g. GET or POST)#
  • post_data <str|bytes> If set changes the post data of request#
  • url <str> If set changes the request URL. New URL must have same protocol as original one.#
  • returns: <NoneType>#

Continues route's request with optional overrides.

def handle(route, request):
# override headers
headers = {
**request.headers,
"foo": "bar" # set "foo" header
"origin": None # remove "origin" header
}
route.continue_(headers=headers)
}
page.route("**/*", handle)

route.fulfill(**kwargs)

  • body <str|bytes> Response body.#
  • content_type <str> If set, equals to setting Content-Type response header.#
  • headers <Dict[str, str]> Response headers. Header values will be converted to a string.#
  • path <Union[str, pathlib.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.#
  • response <APIResponse> APIResponse to fulfill route's request with. Individual fields of the response (such as headers) can be overridden using fulfill options.#
  • status <int> Response status code, defaults to 200.#
  • returns: <NoneType>#

Fulfills route's request with given response.

An example of fulfilling all requests with 404 responses:

page.route("**/*", lambda route: route.fulfill(
status=404,
content_type="text/plain",
body="not found!"))

An example of serving static file:

page.route("**/xhr_endpoint", lambda route: route.fulfill(path="mock_data.json"))

route.request

A request to be routed.