Skip to main content


公开可用于 Web API 测试的 API。该类用于创建 APIRequestContext 实例,该实例又可用于发送 Web 请求。该类的实例可以通过 playwright.request 获得。欲了解更多信息,请参阅 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.




Added in: v1.16 apiRequest.newContext

创建 APIRequestContext 的新实例。

Creates new instances of APIRequestContext.



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



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 in http://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 in http://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 in http://localhost:3000/bar.html

包含随每个请求发送的附加 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).

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 代理。

Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example or socks5:// Short form is considered an HTTP proxy.

用于绕过代理的可选逗号分隔域,例如 ".com,,"

Optional comma-separated domains to bypass proxy, for example ".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.