HTTPRequest.respond() 方法
用给定的响应完成请求。
签名
class HTTPRequest {
respond(
response: Partial<ResponseForRequest>,
priority?: number,
): Promise<void>;
}
参数
|
参数 |
类型 |
说明 |
|---|---|---|
|
response |
Partial<ResponseForRequest> |
用于完成请求的响应。 |
|
priority |
number |
(可选) 如果提供了该参数,则按照协作处理规则解析拦截。否则,拦截将立即解析。 |
返回值:
Promise<void>
备注
要使用此方法,需要通过 Page.setRequestInterception() 启用请求拦截。
如果未启用请求拦截,会立即抛出异常。
示例
将所有请求以 404 响应完成的示例:
await page.setRequestInterception(true);
page.on('request', request => {
request.respond({
status: 404,
contentType: 'text/plain',
body: 'Not Found!',
});
});
注意:不支持为 dataURL 请求模拟响应。对 dataURL 请求调用 request.respond 是无效操作。