HTTPRequest.respond() 方法
🌐 HTTPRequest.respond() method
使用给定的响应来满足请求。
🌐 Fulfills a request with the given response.
语法
🌐 Signature
class HTTPRequest {
respond(
response: Partial<ResponseForRequest>,
priority?: number,
): Promise<void>;
}
参数
🌐 Parameters
范围 🌐 Parameter | 类型 🌐 Type | 描述 🌐 Description |
|---|---|---|
response | Partial<ResponseForRequest> | 满足请求的响应。 🌐 the response to fulfill the request with. |
priority | number | (可选) 如果提供,则使用合作处理规则解决拦截。否则,拦截会立即解决。 🌐 (Optional) If provided, intercept is resolved using cooperative handling rules. Otherwise, intercept is resolved immediately. |
返回:
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 将不会执行任何操作。
🌐 NOTE: Mocking responses for dataURL requests is not supported. Calling request.respond for a dataURL request is a noop.