docs: add response interception to network guide (#9598)
This commit is contained in:
parent
1656c7071e
commit
79955fc4a8
|
|
@ -577,6 +577,44 @@ else
|
||||||
|
|
||||||
<br/>
|
<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
|
## WebSockets
|
||||||
|
|
||||||
Playwright supports [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) inspection out of the
|
Playwright supports [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) inspection out of the
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue