2021-01-07 11:46:05 -08:00
|
|
|
# class: Route
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
Whenever a network route is set up with [`method: Page.route`] or [`method: BrowserContext.route`], the `Route` object
|
|
|
|
allows to handle the route.
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
## async method: Route.abort
|
|
|
|
|
|
|
|
Aborts the route's request.
|
|
|
|
|
|
|
|
### param: Route.abort.errorCode
|
|
|
|
- `errorCode` <[string]>
|
|
|
|
|
|
|
|
Optional error code. Defaults to `failed`, could be one of the following:
|
2021-01-14 07:48:56 -08:00
|
|
|
* `'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.
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
## async method: Route.continue
|
2021-01-08 15:00:14 -08:00
|
|
|
* langs:
|
|
|
|
- alias-python: continue_
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
Continues route's request with optional overrides.
|
|
|
|
|
|
|
|
```js
|
|
|
|
await page.route('**/*', (route, request) => {
|
|
|
|
// Override headers
|
|
|
|
const headers = {
|
|
|
|
...request.headers(),
|
|
|
|
foo: 'bar', // set "foo" header
|
|
|
|
origin: undefined, // remove "origin" header
|
|
|
|
};
|
|
|
|
route.continue({headers});
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
```python async
|
|
|
|
async def handle(route, request):
|
|
|
|
# override headers
|
|
|
|
headers = {
|
|
|
|
**request.headers,
|
|
|
|
"foo": "bar" # set "foo" header
|
|
|
|
"origin": None # remove "origin" header
|
|
|
|
}
|
|
|
|
await route.continue(headers=headers)
|
|
|
|
}
|
|
|
|
await page.route("**/*", handle)
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
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)
|
|
|
|
```
|
|
|
|
|
2021-01-08 16:17:54 -08:00
|
|
|
### option: Route.continue.url
|
|
|
|
- `url` <[string]>
|
|
|
|
|
|
|
|
If set changes the request URL. New URL must have same protocol as original one.
|
|
|
|
|
|
|
|
### option: Route.continue.method
|
|
|
|
- `method` <[string]>
|
|
|
|
|
|
|
|
If set changes the request method (e.g. GET or POST)
|
|
|
|
|
|
|
|
### option: Route.continue.postData
|
|
|
|
- `postData` <[string]|[Buffer]>
|
|
|
|
|
|
|
|
If set changes the post data of request
|
|
|
|
|
|
|
|
### option: Route.continue.headers
|
|
|
|
- `headers` <[Object]<[string], [string]>>
|
|
|
|
|
|
|
|
If set changes the request HTTP headers. Header values will be converted to a string.
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
## async method: Route.fulfill
|
|
|
|
|
|
|
|
Fulfills route's request with given response.
|
|
|
|
|
|
|
|
An example of fulfilling all requests with 404 responses:
|
|
|
|
|
|
|
|
```js
|
|
|
|
await page.route('**/*', route => {
|
|
|
|
route.fulfill({
|
|
|
|
status: 404,
|
|
|
|
contentType: 'text/plain',
|
|
|
|
body: 'Not Found!'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
```python async
|
|
|
|
await page.route("**/*", lambda route: route.fulfill(
|
|
|
|
status=404,
|
|
|
|
content_type="text/plain",
|
|
|
|
body="not found!"))
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
page.route("**/*", lambda route: route.fulfill(
|
|
|
|
status=404,
|
|
|
|
content_type="text/plain",
|
|
|
|
body="not found!"))
|
|
|
|
```
|
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
An example of serving static file:
|
|
|
|
|
|
|
|
```js
|
|
|
|
await page.route('**/xhr_endpoint', route => route.fulfill({ path: 'mock_data.json' }));
|
|
|
|
```
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
```python async
|
|
|
|
await page.route("**/xhr_endpoint", lambda route: route.fulfill(path="mock_data.json"))
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
page.route("**/xhr_endpoint", lambda route: route.fulfill(path="mock_data.json"))
|
|
|
|
```
|
|
|
|
|
2021-01-08 16:17:54 -08:00
|
|
|
### option: Route.fulfill.status
|
|
|
|
- `status` <[int]>
|
|
|
|
|
|
|
|
Response status code, defaults to `200`.
|
|
|
|
|
|
|
|
### option: Route.fulfill.headers
|
|
|
|
- `headers` <[Object]<[string], [string]>>
|
|
|
|
|
|
|
|
Response headers. Header values will be converted to a string.
|
|
|
|
|
|
|
|
### option: Route.fulfill.contentType
|
|
|
|
- `contentType` <[string]>
|
|
|
|
|
|
|
|
If set, equals to setting `Content-Type` response header.
|
|
|
|
|
|
|
|
### option: Route.fulfill.body
|
|
|
|
- `body` <[string]|[Buffer]>
|
|
|
|
|
|
|
|
Response body.
|
|
|
|
|
|
|
|
### option: Route.fulfill.path
|
|
|
|
- `path` <[path]>
|
2021-01-07 11:46:05 -08:00
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
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.
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
## method: Route.request
|
|
|
|
- returns: <[Request]>
|
|
|
|
|
|
|
|
A request to be routed.
|