跳至主要内容

APIRequest

暴露可用于Web API测试的API。该类用于创建APIRequestContext实例,该实例又可用于发送Web请求。可以通过playwright.request获取此类的实例。更多信息请参阅APIRequestContext


方法

newContext

Added in: v1.16 apiRequest.newContext

创建APIRequestContext的新实例。

用法

await apiRequest.newContext();
await apiRequest.newContext(options);

参数

  • options Object (optional)
    • baseURL string (可选)#

      apiRequestContext.get()这样的方法会通过使用URL()构造函数来构建对应的URL,同时考虑基础URL。例如:

      • baseURL: http://localhost:3000 and sending request to /bar.html results in http://localhost:3000/bar.html
      • baseURL: http://localhost:3000/foo/ and sending request to ./bar.html results in http://localhost:3000/foo/bar.html
      • baseURL: http://localhost:3000/foo (without trailing slash) and navigating to ./bar.html results in http://localhost:3000/bar.html
    • clientCertificates Array<Object> (可选) 添加于: 1.46#

      • origin string

        证书有效的精确来源。来源包括https协议、主机名以及可选的端口号。

      • certPath string (可选)

        包含PEM格式证书的文件路径。

      • cert Buffer (可选)

        PEM格式证书的直接值。

      • keyPath string (可选)

        包含PEM格式私钥的文件路径。

      • key Buffer (可选)

        PEM格式私钥的直接值。

      • pfxPath string (可选)

        PFX或PKCS12编码的私钥和证书链的路径。

      • pfx Buffer (可选)

        PFX或PKCS12编码的私钥和证书链的直接值。

      • passphrase string (可选)

        私钥(PEM或PFX格式)的密码短语。

      TLS客户端认证允许服务器请求客户端证书并进行验证。

      详情

      要使用的客户端证书数组。每个证书对象必须包含certPathkeyPath两者,或者单独的pfxPath,或者它们对应的直接值等价物(certkey,或者pfx)。如果证书已加密,可以选择提供passphrase属性。origin属性应提供与证书适用的请求来源完全匹配的值。

      note

      在macOS上使用WebKit时,访问localhost将无法获取客户端证书。您可以通过将localhost替换为local.playwright来解决此问题。

    • extraHTTPHeaders Object<string, string> (可选)#

      包含随每个请求发送的额外HTTP头信息的对象。默认为无。

    • failOnStatusCode boolean (可选) 添加于: v1.51#

      是否在响应码非2xx和3xx时抛出异常。默认情况下会返回所有状态码的响应对象。

    • httpCredentials Object (可选)#

      • username string

      • password string

      • origin string (可选)

        限制在特定来源(scheme://host:port)上发送HTTP凭据。

      • send "unauthorized" | "always" (可选)

        此选项仅适用于从对应APIRequestContext发送的请求,不会影响从浏览器发送的请求。'always' - 每个API请求都会附带包含基本认证凭据的Authorization头部。'unauthorized - 仅当收到带有WWW-Authenticate头部的401(未授权)响应时才发送凭据。默认为'unauthorized'

      HTTP认证的凭据。如果未指定来源,则在收到未经授权的响应时,用户名和密码将被发送到任何服务器。

    • ignoreHTTPSErrors boolean (可选)#

      发送网络请求时是否忽略HTTPS错误。默认为false

    • proxy Object (可选)#

      • server string

        用于所有请求的代理服务器。支持HTTP和SOCKS代理,例如http://myproxy.com:3128socks5://myproxy.com:3128。短格式myproxy.com:3128将被视为HTTP代理。

      • bypass string (可选)

        可选的逗号分隔域名以绕过代理,例如 ".com, chromium.org, .domain.com"

      • username string (可选)

        如果HTTP代理需要认证时使用的可选用户名。

      • password string (可选)

        如果HTTP代理需要认证时可选的密码。

      网络代理设置。

    • storageState string | Object (可选参数)#

      使用给定的存储状态填充上下文。此选项可用于通过browserContext.storageState()apiRequestContext.storageState()获取的登录信息来初始化上下文。可以是包含已保存存储的文件路径,也可以是browserContext.storageState()apiRequestContext.storageState()方法返回的值。

    • timeout number (可选)#

      等待响应的最长时间,以毫秒为单位。默认为30000(30秒)。传入0表示禁用超时。

    • userAgent string (可选)#

      在此上下文中使用的特定用户代理。

返回