APIRequest
公开可用于 Web API 测试的 API。该类用于创建 [APIRequestContext][APIRequestContext] 实例,该实例又可用于发送 Web 请求。该类的实例可以通过 playwright.request 获得。欲了解更多信息,请参阅 [APIRequestContext][APIRequestContext]。
¥Exposes API that can be used for the Web API testing. This class is used for creating [APIRequestContext] instance which in turn can be used for sending web requests. An instance of this class can be obtained via playwright.request. For more information see [APIRequestContext].
方法
¥Methods
newContext
Added in: v1.16创建 [APIRequestContext][APIRequestContext] 的新实例。
¥Creates new instances of [APIRequestContext].
用法
¥Usage
await apiRequest.newContext();
await apiRequest.newContext(options);
参数
¥Arguments
-
options
[Object] (optional)baseURL
[string] (optional)#
像 apiRequestContext.get() 这样的方法通过使用 URL()
构造函数来构建相应的 URL,从而考虑基本 URL。示例:
¥Methods like apiRequestContext.get() take the base URL into consideration by using the URL()
constructor for building the corresponding URL. Examples:
-
baseURL:
http://localhost:3000
并向/bar.html
发送请求会导致http://localhost:3000/bar.html
¥baseURL:
http://localhost:3000
and sending request to/bar.html
results inhttp://localhost:3000/bar.html
-
baseURL:
http://localhost:3000/foo/
并向./bar.html
发送请求会导致http://localhost:3000/foo/bar.html
¥baseURL:
http://localhost:3000/foo/
and sending request to./bar.html
results inhttp://localhost:3000/foo/bar.html
-
baseURL:
http://localhost:3000/foo
(无尾部斜杠)并导航至./bar.html
将导致http://localhost:3000/bar.html
¥baseURL:
http://localhost:3000/foo
(without trailing slash) and navigating to./bar.html
results inhttp://localhost:3000/bar.html
证书有效的确切来源。Origin 包括 https
协议、主机名和可选的端口。
¥Exact origin that the certificate is valid for. Origin includes https
protocol, a hostname and optionally a port.
certPath
[string] (optional) Added in: 1.46#
PEM 格式的证书文件路径。
¥Path to the file with the certificate in PEM format.
keyPath
[string] (optional) Added in: 1.46#
PEM 格式的私钥文件路径。
¥Path to the file with the private key in PEM format.
pfxPath
[string] (optional) Added in: 1.46#
PFX 或 PKCS12 编码的私钥和证书链的路径。
¥Path to the PFX or PKCS12 encoded private key and certificate chain.
passphrase
[string] (optional) Added in: 1.46#
私钥的密码(PEM 或 PFX)。
¥Passphrase for the private key (PEM or PFX).
TLS 客户端身份验证允许服务器请求客户端证书并对其进行验证。
¥TLS Client Authentication allows the server to request a client certificate and verify it.
细节
¥Details
要使用的客户端证书数组。每个证书对象必须同时具有 certPath
和 keyPath
或单个 pfxPath
才能加载客户端证书。如果证书已加密,则可选地提供 passphrase
属性。origin
属性应与证书有效的请求来源完全匹配。
¥An array of client certificates to be used. Each certificate object must have both certPath
and keyPath
or a single pfxPath
to load the client certificate. Optionally, passphrase
property should be provided if the certficiate is encrypted. The origin
property should be provided with an exact match to the request origin that the certificate is valid for.
Using Client Certificates in combination with Proxy Servers is not supported.
localhost
will not pick up client certificates. You can make it work by replacing localhost
with local.playwright
.
:::在 macOS 上使用 WebKit 时,访问
包含随每个请求发送的附加 HTTP 标头的对象。默认为无。
¥An object containing additional HTTP headers to be sent with every request. Defaults to none.
限制在特定来源 (scheme://host:port) 上发送 http 凭据。
¥Restrain sending http credentials on specific origin (scheme://host:port).
-
send
"unauthorized" | "always"(可选)#¥
send
"unauthorized" | "always" (optional)#此选项仅适用于从相应 APIRequestContext 发送的请求,不影响从浏览器发送的请求。
'always'
- 带有基本身份验证凭据的Authorization
标头将随每个 API 请求一起发送。'unauthorized
- 仅当收到带有WWW-Authenticate
标头的 401(未授权)响应时才会发送凭据。默认为'unauthorized'
。¥This option only applies to the requests sent from corresponding APIRequestContext and does not affect requests sent from the browser.
'always'
-Authorization
header with basic authentication credentials will be sent with the each API request.'unauthorized
- the credentials are only sent when 401 (Unauthorized) response withWWW-Authenticate
header is received. Defaults to'unauthorized'
.
HTTP 认证 的凭证。如果未指定来源,则用户名和密码将在未经授权的响应时发送到任何服务器。
¥Credentials for HTTP authentication. If no origin is specified, the username and password are sent to any servers upon unauthorized responses.
发送网络请求时是否忽略 HTTPS 错误。默认为 false
。
¥Whether to ignore HTTPS errors when sending network requests. Defaults to false
.
用于所有请求的代理。支持 HTTP 和 SOCKS 代理,例如 http://myproxy.com:3128
或 socks5://myproxy.com:3128
。缩写形式 myproxy.com:3128
被视为 HTTP 代理。
¥Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example http://myproxy.com:3128
or socks5://myproxy.com:3128
. Short form myproxy.com:3128
is considered an HTTP proxy.
用于绕过代理的可选逗号分隔域,例如 ".com, chromium.org, .domain.com"
。
¥Optional comma-separated domains to bypass proxy, for example ".com, chromium.org, .domain.com"
.
如果 HTTP 代理需要身份验证,则使用可选的用户名。
¥Optional username to use if HTTP proxy requires authentication.
如果 HTTP 代理需要身份验证,则使用可选密码。
¥Optional password to use if HTTP proxy requires authentication.
网络代理设置。
¥Network proxy settings.
Unix 时间以秒为单位。
¥Unix time in seconds.
使用给定的存储状态填充上下文。该选项可用于使用通过 browserContext.storageState() 或 apiRequestContext.storageState() 获取的登录信息来初始化上下文。具有已保存存储空间的文件的路径,或者 browserContext.storageState() 或 apiRequestContext.storageState() 方法之一返回的值。
¥Populates context with given storage state. This option can be used to initialize context with logged-in information obtained via browserContext.storageState() or apiRequestContext.storageState(). Either a path to the file with saved storage, or the value returned by one of browserContext.storageState() or apiRequestContext.storageState() methods.
等待响应的最长时间(以毫秒为单位)。默认为 30000
(30 秒)。通过 0
禁用超时。
¥Maximum time in milliseconds to wait for the response. Defaults to 30000
(30 seconds). Pass 0
to disable timeout.
在此上下文中使用的特定用户代理。
¥Specific user agent to use in this context.
返回
¥Returns