Skip to main content
Version: 24.38.0

HTTPRequest 类

🌐 HTTPRequest class

表示页面发送的 HTTP 请求。

🌐 Represents an HTTP request sent by a page.

语法

🌐 Signature

export declare abstract class HTTPRequest

附注

🌐 Remarks

每当页面发送请求(例如请求网络资源)时,Puppeteer 的 page 会触发以下事件:

🌐 Whenever the page sends a request, such as for a network resource, the following events are emitted by Puppeteer's page:

  • request:当页面发送请求时触发。
  • requestfinished - 当下载响应正文并且请求完成时触发。

如果请求在某个阶段失败,那么会发送 requestfailed 事件,而不是 requestfinished 事件。

🌐 If request fails at some point, then instead of requestfinished event the requestfailed event is emitted.

所有这些事件都提供了一个 HTTPRequest 表示发生的请求的实例:

🌐 All of these events provide an instance of HTTPRequest representing the request that occurred:

page.on('request', request => ...)

注意:HTTP 错误响应,例如 404 或 503,从 HTTP 的角度来看仍然是成功的响应,因此请求将以 requestfinished 事件完成。

🌐 NOTE: HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will complete with requestfinished event.

如果请求收到“重定向”响应,请求将通过 requestfinished 事件成功完成,并且会向重定向的 URL 发出一个新的请求。

🌐 If request gets a 'redirect' response, the request is successfully finished with the requestfinished event, and a new request is issued to a redirected url.

这个类的构造函数被标记为内部。第三方代码不应直接调用构造函数或创建继承 HTTPRequest 类的子类。

🌐 The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the HTTPRequest class.

属性

🌐 Properties

属性

🌐 Property

修饰符

🌐 Modifiers

类型

🌐 Type

描述

🌐 Description

client

readonly

CDPSession

(实验性) 警告!使用此客户端可能会破坏 Puppeteer。请谨慎使用。

方法

🌐 Methods

方法

🌐 Method

修饰符

🌐 Modifiers

描述

🌐 Description

abort(errorCode, priority)

中止请求。

🌐 Aborts a request.

附注

要使用此功能,应通过 Page.setRequestInterception() 启用请求拦截。如果未启用,此方法将立即抛出异常。

🌐 To use this, request interception should be enabled with Page.setRequestInterception(). If it is not enabled, this method will throw an exception immediately.

abortErrorReason()

最近中止请求的原因

🌐 The most recent reason for aborting the request

continue(overrides, priority)

通过可选的请求覆盖继续请求。

🌐 Continues request with optional request overrides.

附注

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

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

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

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

continueRequestOverrides()

如果允许拦截继续(即没有调用 abort()respond()),将使用 ContinueRequestOverrides

🌐 The ContinueRequestOverrides that will be used if the interception is allowed to continue (ie, abort() and respond() aren't called).

enqueueInterceptAction(pendingHandler)

将一个异步请求处理程序添加到处理队列中。延迟处理程序不能保证按特定顺序执行,但它们保证在请求拦截完成之前解决。

🌐 Adds an async request handler to the processing queue. Deferred handlers are not guaranteed to execute in any particular order, but they are guaranteed to resolve before the request interception is finalized.

failure()

访问有关请求失败的信息。

🌐 Access information about the request's failure.

附注

fetchPostData()

从浏览器获取请求的 POST 数据。

🌐 Fetches the POST data for the request from the browser.

finalizeInterceptions()

等待挂起的拦截处理程序,然后决定如何完成请求拦截。

🌐 Awaits pending interception handlers and then decides how to fulfill the request interception.

frame()

发起请求的框架,如果导航到错误页面,则为 null。

🌐 The frame that initiated the request, or null if navigating to error pages.

hasPostData()

当请求包含 POST 数据时为真。请注意,当数据过长或无法以解码形式立即获取时,即使此标志为真,HTTPRequest.postData() 仍可能未定义。在这种情况下,请使用 HTTPRequest.fetchPostData()

🌐 True when the request has POST data. Note that HTTPRequest.postData() might still be undefined when this flag is true when the data is too long or not readily available in the decoded form. In that case, use HTTPRequest.fetchPostData().

headers()

一个与请求相关联的 HTTP 头对象。所有头名称都是小写。

🌐 An object with HTTP headers associated with the request. All header names are lower-case.

initiator()

请求的发起者。

🌐 The initiator of the request.

interceptResolutionState()

描述当前解析操作和优先级的 InterceptResolutionState 对象。

🌐 An InterceptResolutionState object describing the current resolution action and priority.

InterceptResolutionState 包含:action: InterceptResolutionAction priority?: 数字

🌐 InterceptResolutionState contains: action: InterceptResolutionAction priority?: number

InterceptResolutionAction 是以下之一:abortrespondcontinuedisablednonealready-handled

🌐 InterceptResolutionAction is one of: abort, respond, continue, disabled, none, or already-handled.

isInterceptResolutionHandled()

如果截距解析已经处理,则为 true,否则为 false

🌐 Is true if the intercept resolution has already been handled, false otherwise.

isNavigationRequest()

如果请求是当前帧导航的驱动程序,则为 True。

🌐 True if the request is the driver of the current frame's navigation.

method()

使用的方法(GETPOST等)

🌐 The method used (GET, POST, etc.)

postData()

deprecated

已弃用:

使用 HTTPRequest.fetchPostData()

🌐 Use HTTPRequest.fetchPostData().

redirectChain()

redirectChain 是为获取资源而发起的一系列请求链。

🌐 A redirectChain is a chain of requests initiated to fetch a resource.

附注

redirectChain 在同一链的所有请求之间共享。

例如,如果网站 http://example.com 有一个指向 https://example.com 的单一重定向,那么链中将包含一个请求:

🌐 For example, if the website http://example.com has a single redirect to https://example.com, then the chain will contain one request:

const response = await page.goto('http://example.com');
const chain = response.request().redirectChain();
console.log(chain.length); // 1
console.log(chain[0].url()); // 'http://example.com'

如果网站 https://google.com 没有重定向,那么链将为空:

🌐 If the website https://google.com has no redirects, then the chain will be empty:

const response = await page.goto('https://google.com');
const chain = response.request().redirectChain();
console.log(chain.length); // 0
resourceType()

包含渲染引擎感知到的请求的资源类型。

🌐 Contains the request's resource type as it was perceived by the rendering engine.

respond(response, priority)

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

🌐 Fulfills a request with the given response.

附注

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

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

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

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

response()

一个匹配的 HTTPResponse 对象,如果响应尚未接收,则为 null。

🌐 A matching HTTPResponse object, or null if the response has not been received yet.

responseForRequest()

如果允许拦截器响应(即没有调用 abort()),将使用 ResponseForRequest

🌐 The ResponseForRequest that gets used if the interception is allowed to respond (ie, abort() is not called).

url()

请求的 URL

🌐 The URL of the request