class Playwright::Route
Learn more about [networking](../network.md).
allows to handle the route.
Whenever a network route is set up with [‘method: Page.route`] or [`method: BrowserContext.route`], the `Route` object
def abort(errorCode: nil)
def abort(errorCode: nil) wrap_impl(@impl.abort(errorCode: unwrap_impl(errorCode))) end
def continue(headers: nil, method: nil, postData: nil, url: nil)
**Details**
```
page.route("**/*", handle)
route.continue_(headers=headers)
}
"bar": None # remove "bar" header
"foo": "foo-value", # set "foo" header
**request.headers,
headers = {
# override headers
def handle(route, request):
```python sync
**Usage**
Continues route's request with optional overrides.
def continue(headers: nil, method: nil, postData: nil, url: nil) wrap_impl(@impl.continue(headers: unwrap_impl(headers), method: unwrap_impl(method), postData: unwrap_impl(postData), url: unwrap_impl(url))) end
def event_emitter_proxy
def event_emitter_proxy _emitter_proxy ||= EventEmitterProxy.new(self, @impl)
def fallback(headers: nil, method: nil, postData: nil, url: nil)
page.route("**/*", handle)
route.fallback(headers=headers)
}
"bar": None # remove "bar" header
"foo": "foo-value", # set "foo" header
**request.headers,
headers = {
# override headers
def handle(route, request):
```python sync
route handler can modify url, method, headers and postData of the request.
One can also modify request while falling back to the subsequent handler, that way intermediate
```
page.route("**/*", handle_post)
page.route("**/*", handle_get)
# ...
# Handling POST only.
return
route.fallback()
if route.request.method != "POST":
def handle_post(route):
# Handle POST requests.
# ...
# Handling GET only.
return
route.fallback()
if route.request.method != "GET":
def handle_get(route):
# Handle GET requests.
```python sync
GET requests vs POST requests as in the example below.
handle different kinds of requests, for example API calls vs page resources or
Registering multiple routes is useful when you want separate handlers to
```
page.route("**/*", lambda route: route.fallback()) # Runs first.
page.route("**/*", lambda route: route.fallback()) # Runs second.
page.route("**/*", lambda route: route.abort()) # Runs last.
```python sync
**Usage**
in the end will be aborted by the first registered route.
request will be handled by the bottom-most handler first, then it'll fall back to the previous one and
That way the last registered route can always override all the previous ones. In the example below,
When several routes match the given pattern, they run in the order opposite to their registration.
def fallback(headers: nil, method: nil, postData: nil, url: nil) wrap_impl(@impl.fallback(headers: unwrap_impl(headers), method: unwrap_impl(method), postData: unwrap_impl(postData), url: unwrap_impl(url))) end
def fetch(
**Details**
```
page.route("https://dog.ceo/api/breeds/list/all", handle)
route.fulfill(response=response, json=json)
json["message"]["big_red_dog"] = []
json = response.json()
response = route.fetch()
def handle(route):
```python sync
**Usage**
could be modified and then fulfilled.
Performs the request and fetches result without fulfilling it, so that the response
def fetch( headers: nil, maxRedirects: nil, method: nil, postData: nil, timeout: nil, url: nil) wrap_impl(@impl.fetch(headers: unwrap_impl(headers), maxRedirects: unwrap_impl(maxRedirects), method: unwrap_impl(method), postData: unwrap_impl(postData), timeout: unwrap_impl(timeout), url: unwrap_impl(url))) end
def fulfill(
page.route("**/xhr_endpoint", lambda route: route.fulfill(path="mock_data.json"))
```python sync
An example of serving static file:
```
body="not found!"))
content_type="text/plain",
status=404,
page.route("**/*", lambda route: route.fulfill(
```python sync
An example of fulfilling all requests with 404 responses:
**Usage**
Fulfills route's request with given response.
def fulfill( body: nil, contentType: nil, headers: nil, json: nil, path: nil, response: nil, status: nil) wrap_impl(@impl.fulfill(body: unwrap_impl(body), contentType: unwrap_impl(contentType), headers: unwrap_impl(headers), json: unwrap_impl(json), path: unwrap_impl(path), response: unwrap_impl(response), status: unwrap_impl(status))) end
def off(event, callback)
-- inherited from EventEmitter --
def off(event, callback) event_emitter_proxy.off(event, callback) end
def on(event, callback)
-- inherited from EventEmitter --
def on(event, callback) event_emitter_proxy.on(event, callback) end
def once(event, callback)
-- inherited from EventEmitter --
def once(event, callback) event_emitter_proxy.once(event, callback) end
def redirect_navigation_request(url)
def redirect_navigation_request(url) wrap_impl(@impl.redirect_navigation_request(unwrap_impl(url))) end
def request
def request wrap_impl(@impl.request) end