Skip to main content

APIResponse

APIResponse 类表示由 apiRequestContext.get() 及类似方法返回的响应。


方法

🌐 Methods

body

Added in: v1.16 apiResponse.body

返回带有响应正文的缓冲区。

🌐 Returns the buffer with response body.

用法

await apiResponse.body();

返回


dispose

Added in: v1.16 apiResponse.dispose

释放此响应的主体。如果不调用此方法,主体将一直保留在内存中,直到上下文关闭。

🌐 Disposes the body of this response. If not called then the body will stay in memory until the context closes.

用法

await apiResponse.dispose();

返回


headers

Added in: v1.16 apiResponse.headers

包含与此响应关联的所有响应 HTTP 标头的对象。

🌐 An object with all the response HTTP headers associated with this response.

用法

apiResponse.headers();

返回


headersArray

Added in: v1.16 apiResponse.headersArray

一个包含与此响应相关的所有 HTTP 响应头的数组。头名称不会转换为小写。具有多个条目的头,例如 Set-Cookie,会在数组中出现多次。

🌐 An array with all the response HTTP headers associated with this response. Header names are not lower-cased. Headers with multiple entries, such as Set-Cookie, appear in the array multiple times.

用法

apiResponse.headersArray();

返回


json

Added in: v1.16 apiResponse.json

返回响应正文的 JSON 表示形式。

🌐 Returns the JSON representation of response body.

如果响应正文无法通过 JSON.parse 解析,该方法将抛出异常。

🌐 This method will throw if the response body is not parsable via JSON.parse.

用法

await apiResponse.json();

返回


ok

Added in: v1.16 apiResponse.ok

包含一个布尔值,说明响应是否成功(状态范围为 200-299)。

🌐 Contains a boolean stating whether the response was successful (status in the range 200-299) or not.

用法

apiResponse.ok();

返回


securityDetails

Added in: v1.61 apiResponse.securityDetails

返回 SSL 及其他安全信息。对于非 HTTPS 响应,会解析为 null。对于重定向的请求,返回重定向链中最后一次请求的信息。

🌐 Returns SSL and other security information. Resolves to null for non-HTTPS responses. For redirected requests, returns the information for the last request in the redirect chain.

用法

await apiResponse.securityDetails();

返回

  • Promise<null | Object>#
    • issuer string (optional)

      证书中发行者字段的常用名称组件。此信息仅用于参考。可选。

    • protocol string (optional)

      使用的特定 TLS 协议。(例如 TLS 1.3)。可选。

    • subjectName string (optional)

      证书中主题字段的通用名称组件。此项仅供参考。可选。

    • validFrom number (optional)

      Unix 时间戳(以秒为单位),指定此证书何时生效。可选。

    • validTo number (optional)

      Unix 时间戳(以秒为单位),指定此证书何时失效。可选。


serverAddr

Added in: v1.61 apiResponse.serverAddr

返回服务器的IP地址和端口。如果服务器地址不可用,则解析为 null。对于重定向请求,返回重定向链中最后一次请求的信息。

🌐 Returns the IP address and port of the server. Resolves to null if the server address is not available. For redirected requests, returns the information for the last request in the redirect chain.

用法

await apiResponse.serverAddr();

返回


status

Added in: v1.16 apiResponse.status

包含响应的状态代码(例如,200 表示成功)。

🌐 Contains the status code of the response (e.g., 200 for a success).

用法

apiResponse.status();

返回


statusText

Added in: v1.16 apiResponse.statusText

包含响应的状态文本(例如,通常成功时为“OK”)。

🌐 Contains the status text of the response (e.g. usually an "OK" for a success).

用法

apiResponse.statusText();

返回


text

Added in: v1.16 apiResponse.text

返回响应正文的文本表示形式。

🌐 Returns the text representation of response body.

用法

await apiResponse.text();

返回


timing

Added in: v1.62 apiResponse.timing

返回给定响应的资源时序信息。对于重定向的请求,返回重定向链中最后一个请求的信息。当响应是从 HAR 文件 提供时,时序信息不可用,所有值都为 -1。更多信息请查看 Resource Timing API

🌐 Returns resource timing information for given response. For redirected requests, returns the information for the last request in the redirect chain. When the response is served from the HAR file, timing information is not available and all the values are -1. Find more information at Resource Timing API.

用法

apiResponse.timing();

返回

  • Object#
    • startTime number

      自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的请求开始时间(以毫秒为单位)

    • domainLookupStart number

      客户端开始查找资源的域名之前的时间。这个值以相对于 startTime 的毫秒为单位,如果不可用则为 -1。

    • domainLookupEnd number

      客户端结束资源的域名查询后的时间。该值以相对于 startTime 的毫秒为单位,如果不可用则为 -1。

    • connectStart number

      客户端开始建立与服务器的连接以获取资源之前的时间。该值以相对于 startTime 的毫秒为单位表示,如果不可用则为 -1。

    • secureConnectionStart number

      客户端开始握手进程以保护当前连接之前的时间。该值以相对于 startTime 的毫秒为单位表示,如果不可用则为 -1。

    • connectEnd number

      客户端建立与服务器的连接以获取资源后的时间。该值以相对于 startTime 的毫秒为单位,如果不可用则为 -1。

    • requestStart number

      客户端开始从服务器、缓存或本地资源请求资源之前的时间。该值以相对于 startTime 的毫秒为单位,如果不可用则为 -1。

    • responseStart number

      客户端在收到服务器、缓存或本地资源的响应的第一个字节后的时间。该值以相对于 startTime 的毫秒为单位,如果不可用则为 -1。

    • responseEnd number

      在客户端接收资源的最后一个字节后立即,或者在传输连接关闭前立即,以先发生者为准。值以相对于 startTime 的毫秒为单位,如果不可用则为 -1。


url

Added in: v1.16 apiResponse.url

包含响应的 URL。

🌐 Contains the URL of the response.

用法

apiResponse.url();

返回