docs: add response interception to network guide (#9598)

This commit is contained in:
Yury Semikhatsky 2021-10-18 17:53:06 -07:00 committed by GitHub
parent 1656c7071e
commit 79955fc4a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -577,6 +577,44 @@ else
<br/>
## Modify responses
* langs: js
To modify a response use [ApiRequestContext] to get original response and then pass the response to [`method: Route.fulfill`].
You can override individual fields on the reponse via options:
```js
await page.route('**/title.html', async route => {
// Fetch original response.
const response = await page.request.fetch(route.request());
// Add a prefix to the title.
let body = await response.text();
body = body.replace('<title>', '<title>My prefix:');
route.fulfill({
// Pass all fields from the response.
response,
// Override response body.
body,
// Force content type to be html.
headers: {
...response.headers(),
'content-type': 'text/html'
}
});
});
```
### API reference
- [ApiRequestContext]
- [`method: Page.route`]
- [`method: BrowserContext.route`]
- [`property: Playwright.request`]
- [`property: BrowserContext.request`]
- [`property: Page.request`]
- [`method: Route.fulfill`]
<br/>
## WebSockets
Playwright supports [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) inspection out of the