跳至主要内容

APIRequest

提供可用于Web API测试的API。该类用于创建APIRequestContext实例,该实例又可用于发送Web请求。可以通过Playwright.APIRequest获取该类的实例。更多信息请参阅APIRequestContext


方法

NewContextAsync

Added in: v1.16 apiRequest.NewContextAsync

创建APIRequestContext的新实例。

用法

await ApiRequest.NewContextAsync(options);

参数

  • options ApiRequestNewContextOptions? (optional)
    • BaseURL string? (可选)#

      ApiRequestContext.GetAsync()这样的方法会通过使用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 IEnumerable?<ClientCertificates> (可选) 添加于: 1.46#

      • Origin string

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

      • CertPath string? (可选)

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

      • Cert byte[]? (可选)

        证书的直接值,采用PEM格式。

      • KeyPath string? (可选)

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

      • Key byte[]? (可选)

        私钥的直接值,采用PEM格式。

      • PfxPath string? (可选)

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

      • Pfx byte[]? (可选)

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

      • Passphrase string? (可选)

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

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

      详情

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

      note

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

    • ExtraHTTPHeaders IDictionary?<string, string> (可选)#

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

    • FailOnStatusCode bool? (可选) 添加于: v1.51#

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

    • HttpCredentials HttpCredentials? (可选)#

      • Username string

      • Password string

      • Origin string? (可选)

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

      • Send enum HttpCredentialsSend { Unauthorized, Always }? (可选)

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

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

    • IgnoreHTTPSErrors bool? (可选)#

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

    • Proxy 代理? (可选)#

      • 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? (可选)#

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

    • StorageStatePath string? (可选) 添加于: v1.18#

      使用给定的存储状态填充上下文。此选项可用于通过BrowserContext.StorageStateAsync()获取的登录信息初始化上下文。包含已保存存储状态的文件路径。

    • Timeout [float]? (可选)#

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

    • UserAgent string? (可选)#

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

返回