Skip to main content
Version: 23.2.0

HTTPRequest.respond() 方法

¥HTTPRequest.respond() method

使用给定的响应来满足请求。

¥Fulfills a request with the given response.

签名

¥Signature

class HTTPRequest {
respond(
response: Partial<ResponseForRequest>,
priority?: number
): Promise<void>;
}

参数

¥Parameters

范围

类型

描述

响应

部分<ResponseForRequest>

满足请求的响应。

priority

数字

(可选的)如果提供,则使用协作处理规则来解决拦截。否则,拦截将立即解决。

Returns:

Promise<void>

备注

¥Remarks

要使用此功能,应使用 Page.setRequestInterception() 启用请求拦截。

¥To use this, request interception should be enabled with Page.setRequestInterception().

如果未启用请求拦截,则立即抛出异常。

¥Exception is immediately thrown if the request interception is not enabled.

示例

¥Example

使用 404 响应满足所有请求的示例:

¥An example of fulfilling all requests with 404 responses:

await page.setRequestInterception(true);
page.on('request', request => {
request.respond({
status: 404,
contentType: 'text/plain',
body: 'Not Found!',
});
});

注意:不支持 dataURL 请求的模拟响应。为 dataURL 请求调用 request.respond 是一个 noop。

¥NOTE: Mocking responses for dataURL requests is not supported. Calling request.respond for a dataURL request is a noop.