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.request(). For more information see APIRequestContext.
Methods
newContext
Added in: v1.16Creates new instances of APIRequestContext.
Usage
APIRequest.newContext();
APIRequest.newContext(options);
Arguments
options
ApiRequest.NewContextOptions
(optional)-
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
and sending request to/bar.html
results inhttp://localhost:3000/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
(without trailing slash) and navigating to./bar.html
results inhttp://localhost:3000/bar.html
- baseURL:
-
setClientCertificates
List<ClientCertificates> (optional) Added in: 1.46#-
setOrigin
StringExact origin that the certificate is valid for. Origin includes
https
protocol, a hostname and optionally a port. -
setCertPath
Path (optional)Path to the file with the certificate in PEM format.
-
setCert
byte[] (optional)Direct value of the certificate in PEM format.
-
setKeyPath
Path (optional)Path to the file with the private key in PEM format.
-
setKey
byte[] (optional)Direct value of the private key in PEM format.
-
setPfxPath
Path (optional)Path to the PFX or PKCS12 encoded private key and certificate chain.
-
setPfx
byte[] (optional)Direct value of the PFX or PKCS12 encoded private key and certificate chain.
-
setPassphrase
String (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
certPath
andkeyPath
, a singlepfxPath
, or their corresponding direct value equivalents (cert
andkey
, orpfx
). Optionally,passphrase
property should be provided if the certificate is encrypted. Theorigin
property should be provided with an exact match to the request origin that the certificate is valid for.noteUsing Client Certificates in combination with Proxy Servers is not supported.
noteWhen using WebKit on macOS, accessing
localhost
will not pick up client certificates. You can make it work by replacinglocalhost
withlocal.playwright
. -
-
setExtraHTTPHeaders
Map<String, String> (optional)#An object containing additional HTTP headers to be sent with every request. Defaults to none.
-
setHttpCredentials
HttpCredentials (optional)#-
setUsername
String -
setPassword
String -
setOrigin
String (optional)Restrain sending http credentials on specific origin (scheme://host:port).
-
setSend
enum 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'
-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'
.
Credentials for HTTP authentication. If no origin is specified, the username and password are sent to any servers upon unauthorized responses.
-
-
setIgnoreHTTPSErrors
boolean (optional)#Whether to ignore HTTPS errors when sending network requests. Defaults to
false
. -
setProxy
Proxy (optional)#-
setServer
StringProxy to be used for all requests. HTTP and SOCKS proxies are supported, for example
http://myproxy.com:3128
orsocks5://myproxy.com:3128
. Short formmyproxy.com:3128
is considered an HTTP proxy. -
setBypass
String (optional)Optional comma-separated domains to bypass proxy, for example
".com, chromium.org, .domain.com"
. -
setUsername
String (optional)Optional username to use if HTTP proxy requires authentication.
-
setPassword
String (optional)Optional password to use if HTTP proxy requires authentication.
Network proxy settings.
-
-
setStorageState
String (optional)#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.
-
setStorageStatePath
Path (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.storageState(). Path to the file with saved storage state.
-
Maximum time in milliseconds to wait for the response. Defaults to
30000
(30 seconds). Pass0
to disable timeout. -
setUserAgent
String (optional)#Specific user agent to use in this context.
-
Returns