APIRequest
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.APIRequest. For more information see APIRequestContext.
Methods
NewContextAsync
Added in: v1.16Creates new instances of APIRequestContext.
Usage
await ApiRequest.NewContextAsync(options);
Arguments
optionsApiRequestNewContextOptions?(optional)-
Methods like ApiRequestContext.GetAsync() take the base URL into consideration by using the
URL()constructor for building the corresponding URL. Examples:- baseURL:
http://localhost:3000and sending request to/bar.htmlresults inhttp://localhost:3000/bar.html - baseURL:
http://localhost:3000/foo/and sending request to./bar.htmlresults inhttp://localhost:3000/foo/bar.html - baseURL:
http://localhost:3000/foo(without trailing slash) and navigating to./bar.htmlresults inhttp://localhost:3000/bar.html
- baseURL:
-
ClientCertificatesIEnumerable?<ClientCertificates> (optional) Added in: 1.46#-
OriginstringExact origin that the certificate is valid for. Origin includes
httpsprotocol, a hostname and optionally a port. -
CertPathstring? (optional)Path to the file with the certificate in PEM format.
-
Certbyte[]? (optional)Direct value of the certificate in PEM format.
-
KeyPathstring? (optional)Path to the file with the private key in PEM format.
-
Keybyte[]? (optional)Direct value of the private key in PEM format.
-
PfxPathstring? (optional)Path to the PFX or PKCS12 encoded private key and certificate chain.
-
Pfxbyte[]? (optional)Direct value of the PFX or PKCS12 encoded private key and certificate chain.
-
Passphrasestring? (optional)Passphrase for the private key (PEM or PFX).
TLS Client Authentication allows the server to request a client certificate and verify it.
Details
An array of client certificates to be used. Each certificate object must have either both
certPathandkeyPath, a singlepfxPath, or their corresponding direct value equivalents (certandkey, orpfx). Optionally,passphraseproperty should be provided if the certificate is encrypted. Theoriginproperty should be provided with an exact match to the request origin that the certificate is valid for.备注When using WebKit on macOS, accessing
localhostwill not pick up client certificates. You can make it work by replacinglocalhostwithlocal.playwright. -
-
ExtraHTTPHeadersIDictionary?<string, string> (optional)#An object containing additional HTTP headers to be sent with every request. Defaults to none.
-
FailOnStatusCodebool? (optional) Added in: v1.51#Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
-
HttpCredentialsHttpCredentials? (optional)#-
Usernamestring -
Passwordstring -
Originstring? (optional)Restrain sending http credentials on specific origin (scheme://host:port).
-
Sendenum HttpCredentialsSend { Unauthorized, Always }?(optional)This option only applies to the requests sent from corresponding APIRequestContext and does not affect requests sent from the browser.
'always'-Authorizationheader with basic authentication credentials will be sent with the each API request.'unauthorized- the credentials are only sent when 401 (Unauthorized) response withWWW-Authenticateheader is received. Defaults to'unauthorized'.
Credentials for HTTP authentication. If no origin is specified, the username and password are sent to any servers upon unauthorized responses.
-
-
IgnoreHTTPSErrorsbool? (optional)#Whether to ignore HTTPS errors when sending network requests. Defaults to
false. -
MaxRedirectsint? (optional) Added in: v1.52#Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to
20. Pass0to not follow redirects. This can be overwritten for each request individually. -
ProxyProxy? (optional)#-
ServerstringProxy to be used for all requests. HTTP and SOCKS proxies are supported, for example
http://myproxy.com:3128orsocks5://myproxy.com:3128. Short formmyproxy.com:3128is considered an HTTP proxy. -
Bypassstring? (optional)Optional comma-separated domains to bypass proxy, for example
".com, chromium.org, .domain.com". -
Usernamestring? (optional)Optional username to use if HTTP proxy requires authentication.
-
Passwordstring? (optional)Optional password to use if HTTP proxy requires authentication.
Network proxy settings.
-
-
StorageStatestring? (optional)#Populates context with given storage state. This option can be used to initialize context with logged-in information obtained via BrowserContext.StorageStateAsync() or ApiRequestContext.StorageStateAsync(). Either a path to the file with saved storage, or the value returned by one of BrowserContext.StorageStateAsync() or ApiRequestContext.StorageStateAsync() methods.
-
StorageStatePathstring? (optional) Added in: v1.18#Populates context with given storage state. This option can be used to initialize context with logged-in information obtained via BrowserContext.StorageStateAsync(). Path to the file with saved storage state.
-
Timeout[float]? (optional)#Maximum time in milliseconds to wait for the response. Defaults to
30000(30 seconds). Pass0to disable timeout. -
Specific user agent to use in this context.
-
Returns