documentation/docs/api/route
sidebar_position: 10
Route
Whenever a network route is set up with Page#route or BrowserContext#route, the Route object
allows to handle the route.
Learn more about networking.
abort
def abort(errorCode: nil)
Aborts the route’s request.
continue
def continue(headers: nil, method: nil, postData: nil, url: nil)
Continues route’s request with optional overrides.
Usage
def handle(route, request) # override headers headers = request.headers headers['foo'] = 'bar' # set "foo" header headers['user-agent'] = 'Unknown Browser' # modify user-agent headers.delete('bar') # remove "bar" header route.continue(headers: headers) end page.route("**/*", method(:handle))
Details
Note that any overrides such as url
or headers
only apply to the request being routed. If this request results in a redirect, overrides will not be applied to the new redirected request. If you want to propagate a header through redirects, use the combination of Route#fetch and Route#fulfill instead.
fallback
def fallback(headers: nil, method: nil, postData: nil, url: nil)
When several routes match the given pattern, they run in the order opposite to their registration.
That way the last registered route can always override all the previous ones. In the example below,
request will be handled by the bottom-most handler first, then it’ll fall back to the previous one and
in the end will be aborted by the first registered route.
Usage
page.route("**/*", -> (route,_) { route.abort }) # Runs last. page.route("**/*", -> (route,_) { route.fallback }) # Runs second. page.route("**/*", -> (route,_) { route.fallback }) # Runs first.
Registering multiple routes is useful when you want separate handlers to
handle different kinds of requests, for example API calls vs page resources or
GET requests vs POST requests as in the example below.
# Handle GET requests. def handle_post(route, request) if request.method != "GET" route.fallback return end # Handling GET only. # ... end # Handle POST requests. def handle_post(route, request) if request.method != "POST" route.fallback return end # Handling POST only. # ... end page.route("**/*", handle_get) page.route("**/*", handle_post)
One can also modify request while falling back to the subsequent handler, that way intermediate
route handler can modify url, method, headers and postData of the request.
def handle(route, request) # override headers headers = request.headers headers['foo'] = 'bar' # set "foo" header headers['user-agent'] = 'Unknown Browser' # modify user-agent headers.delete('bar') # remove "bar" header route.fallback(headers: headers) end page.route("**/*", method(:handle))
fetch
def fetch( headers: nil, maxRedirects: nil, method: nil, postData: nil, timeout: nil, url: nil)
Performs the request and fetches result without fulfilling it, so that the response
could be modified and then fulfilled.
Usage
def handle(route, request) response = route.fetch json = response.json json["message"]["big_red_dog"] = [] route.fulfill(response: response, json: json) end page.route("https://dog.ceo/api/breeds/list/all", method(:handle))
Details
Note that headers
option will apply to the fetched request as well as any redirects initiated by it. If you want to only apply headers
to the original request, but not to redirects, look into Route#continue instead.
fulfill
def fulfill( body: nil, contentType: nil, headers: nil, json: nil, path: nil, response: nil, status: nil)
Fulfills route’s request with given response.
Usage
An example of fulfilling all requests with 404 responses:
page.route("**/*", ->(route, request) { route.fulfill( status: 404, contentType: 'text/plain', body: 'not found!!', ) })
An example of serving static file:
page.route("**/xhr_endpoint", ->(route, _) { route.fulfill(path: "mock_data.json") })
request
def request
A request to be routed.