Interface APIRequestContext
- All Known Implementing Classes:
APIRequestContextImpl
Each Playwright browser context has associated with it APIRequestContext instance which shares cookie storage
with the browser context and can be accessed via BrowserContext.request() or Page.request(). It is also possible to create a new APIRequestContext instance manually by calling APIRequest.newContext().
**Cookie management**
APIRequestContext returned by BrowserContext.request() and Page.request() shares cookie storage with the corresponding BrowserContext. Each API request will have
Cookie header populated with the values from the browser context. If the API response contains Set-Cookie
header it will automatically update BrowserContext cookies and requests made from the page will pick them up.
This means that if you log in using this API, your e2e test will be logged in and vice versa.
If you want API requests to not interfere with the browser cookies you should create a new APIRequestContext by
calling APIRequest.newContext(). Such APIRequestContext object will have its own
isolated cookie storage.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptiondefault APIResponseSends HTTP(S) DELETE request and returns its response.delete(String url, RequestOptions params) Sends HTTP(S) DELETE request and returns its response.voiddispose()All responses returned byAPIRequestContext.get()and similar methods are stored in the memory, so that you can later callAPIResponse.body().default APIResponseSends HTTP(S) request and returns its response.fetch(Request urlOrRequest, RequestOptions params) Sends HTTP(S) request and returns its response.default APIResponseSends HTTP(S) request and returns its response.fetch(String urlOrRequest, RequestOptions params) Sends HTTP(S) request and returns its response.default APIResponseSends HTTP(S) GET request and returns its response.get(String url, RequestOptions params) Sends HTTP(S) GET request and returns its response.default APIResponseSends HTTP(S) HEAD request and returns its response.head(String url, RequestOptions params) Sends HTTP(S) HEAD request and returns its response.default APIResponseSends HTTP(S) PATCH request and returns its response.patch(String url, RequestOptions params) Sends HTTP(S) PATCH request and returns its response.default APIResponseSends HTTP(S) POST request and returns its response.post(String url, RequestOptions params) Sends HTTP(S) POST request and returns its response.default APIResponseSends HTTP(S) PUT request and returns its response.put(String url, RequestOptions params) Sends HTTP(S) PUT request and returns its response.default StringReturns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.
-
Method Details
-
delete
Sends HTTP(S) DELETE request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.- Parameters:
url- Target URL.- Since:
- v1.16
-
delete
Sends HTTP(S) DELETE request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.- Parameters:
url- Target URL.params- Optional request parameters.- Since:
- v1.16
-
dispose
void dispose()All responses returned byAPIRequestContext.get()and similar methods are stored in the memory, so that you can later callAPIResponse.body(). This method discards all stored responses, and makesAPIResponse.body()throw "Response disposed" error.- Since:
- v1.16
-
fetch
Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects. JSON objects can be passed directly to the request.**Usage**
Map<String, Object> data = new HashMap(); data.put("title", "Book Title"); data.put("body", "John Doe"); request.fetch("https://example.com/api/createBook", RequestOptions.create().setMethod("post").setData(data));The common way to send file(s) in the body of a request is to encode it as form fields with
multipart/form-dataencoding. You can achieve that with Playwright API like this:// Pass file path to the form data constructor: Path file = Paths.get("team.csv"); APIResponse response = request.fetch("https://example.com/api/uploadTeamList", RequestOptions.create().setMethod("post").setMultipart( FormData.create().set("fileField", file))); // Or you can pass the file content directly as FilePayload object: FilePayload filePayload = new FilePayload("f.js", "text/javascript", "console.log(2022);".getBytes(StandardCharsets.UTF_8)); APIResponse response = request.fetch("https://example.com/api/uploadTeamList", RequestOptions.create().setMethod("post").setMultipart( FormData.create().set("fileField", filePayload)));- Parameters:
urlOrRequest- Target URL or Request to get all parameters from.- Since:
- v1.16
-
fetch
Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects. JSON objects can be passed directly to the request.**Usage**
Map<String, Object> data = new HashMap(); data.put("title", "Book Title"); data.put("body", "John Doe"); request.fetch("https://example.com/api/createBook", RequestOptions.create().setMethod("post").setData(data));The common way to send file(s) in the body of a request is to encode it as form fields with
multipart/form-dataencoding. You can achieve that with Playwright API like this:// Pass file path to the form data constructor: Path file = Paths.get("team.csv"); APIResponse response = request.fetch("https://example.com/api/uploadTeamList", RequestOptions.create().setMethod("post").setMultipart( FormData.create().set("fileField", file))); // Or you can pass the file content directly as FilePayload object: FilePayload filePayload = new FilePayload("f.js", "text/javascript", "console.log(2022);".getBytes(StandardCharsets.UTF_8)); APIResponse response = request.fetch("https://example.com/api/uploadTeamList", RequestOptions.create().setMethod("post").setMultipart( FormData.create().set("fileField", filePayload)));- Parameters:
urlOrRequest- Target URL or Request to get all parameters from.params- Optional request parameters.- Since:
- v1.16
-
fetch
Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects. JSON objects can be passed directly to the request.**Usage**
Map<String, Object> data = new HashMap(); data.put("title", "Book Title"); data.put("body", "John Doe"); request.fetch("https://example.com/api/createBook", RequestOptions.create().setMethod("post").setData(data));The common way to send file(s) in the body of a request is to encode it as form fields with
multipart/form-dataencoding. You can achieve that with Playwright API like this:// Pass file path to the form data constructor: Path file = Paths.get("team.csv"); APIResponse response = request.fetch("https://example.com/api/uploadTeamList", RequestOptions.create().setMethod("post").setMultipart( FormData.create().set("fileField", file))); // Or you can pass the file content directly as FilePayload object: FilePayload filePayload = new FilePayload("f.js", "text/javascript", "console.log(2022);".getBytes(StandardCharsets.UTF_8)); APIResponse response = request.fetch("https://example.com/api/uploadTeamList", RequestOptions.create().setMethod("post").setMultipart( FormData.create().set("fileField", filePayload)));- Parameters:
urlOrRequest- Target URL or Request to get all parameters from.- Since:
- v1.16
-
fetch
Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects. JSON objects can be passed directly to the request.**Usage**
Map<String, Object> data = new HashMap(); data.put("title", "Book Title"); data.put("body", "John Doe"); request.fetch("https://example.com/api/createBook", RequestOptions.create().setMethod("post").setData(data));The common way to send file(s) in the body of a request is to encode it as form fields with
multipart/form-dataencoding. You can achieve that with Playwright API like this:// Pass file path to the form data constructor: Path file = Paths.get("team.csv"); APIResponse response = request.fetch("https://example.com/api/uploadTeamList", RequestOptions.create().setMethod("post").setMultipart( FormData.create().set("fileField", file))); // Or you can pass the file content directly as FilePayload object: FilePayload filePayload = new FilePayload("f.js", "text/javascript", "console.log(2022);".getBytes(StandardCharsets.UTF_8)); APIResponse response = request.fetch("https://example.com/api/uploadTeamList", RequestOptions.create().setMethod("post").setMultipart( FormData.create().set("fileField", filePayload)));- Parameters:
urlOrRequest- Target URL or Request to get all parameters from.params- Optional request parameters.- Since:
- v1.16
-
get
Sends HTTP(S) GET request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.**Usage**
Request parameters can be configured with
paramsoption, they will be serialized into the URL search parameters:request.get("https://example.com/api/getText", RequestOptions.create() .setQueryParam("isbn", "1234") .setQueryParam("page", 23));- Parameters:
url- Target URL.- Since:
- v1.16
-
get
Sends HTTP(S) GET request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.**Usage**
Request parameters can be configured with
paramsoption, they will be serialized into the URL search parameters:request.get("https://example.com/api/getText", RequestOptions.create() .setQueryParam("isbn", "1234") .setQueryParam("page", 23));- Parameters:
url- Target URL.params- Optional request parameters.- Since:
- v1.16
-
head
Sends HTTP(S) HEAD request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.- Parameters:
url- Target URL.- Since:
- v1.16
-
head
Sends HTTP(S) HEAD request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.- Parameters:
url- Target URL.params- Optional request parameters.- Since:
- v1.16
-
patch
Sends HTTP(S) PATCH request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.- Parameters:
url- Target URL.- Since:
- v1.16
-
patch
Sends HTTP(S) PATCH request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.- Parameters:
url- Target URL.params- Optional request parameters.- Since:
- v1.16
-
post
Sends HTTP(S) POST request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.**Usage**
JSON objects can be passed directly to the request:
Map<String, Object> data = new HashMap(); data.put("title", "Book Title"); data.put("body", "John Doe"); request.post("https://example.com/api/createBook", RequestOptions.create().setData(data));To send form data to the server use
formoption. Its value will be encoded into the request body withapplication/x-www-form-urlencodedencoding (see below how to usemultipart/form-dataform encoding to send files):request.post("https://example.com/api/findBook", RequestOptions.create().setForm( FormData.create().set("title", "Book Title").set("body", "John Doe") ));The common way to send file(s) in the body of a request is to upload them as form fields with
multipart/form-dataencoding. You can achieve that with Playwright API like this:// Pass file path to the form data constructor: Path file = Paths.get("team.csv"); APIResponse response = request.post("https://example.com/api/uploadTeamList", RequestOptions.create().setMultipart( FormData.create().set("fileField", file))); // Or you can pass the file content directly as FilePayload object: FilePayload filePayload = new FilePayload("f.js", "text/javascript", "console.log(2022);".getBytes(StandardCharsets.UTF_8)); APIResponse response = request.post("https://example.com/api/uploadTeamList", RequestOptions.create().setMultipart( FormData.create().set("fileField", filePayload)));- Parameters:
url- Target URL.- Since:
- v1.16
-
post
Sends HTTP(S) POST request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.**Usage**
JSON objects can be passed directly to the request:
Map<String, Object> data = new HashMap(); data.put("title", "Book Title"); data.put("body", "John Doe"); request.post("https://example.com/api/createBook", RequestOptions.create().setData(data));To send form data to the server use
formoption. Its value will be encoded into the request body withapplication/x-www-form-urlencodedencoding (see below how to usemultipart/form-dataform encoding to send files):request.post("https://example.com/api/findBook", RequestOptions.create().setForm( FormData.create().set("title", "Book Title").set("body", "John Doe") ));The common way to send file(s) in the body of a request is to upload them as form fields with
multipart/form-dataencoding. You can achieve that with Playwright API like this:// Pass file path to the form data constructor: Path file = Paths.get("team.csv"); APIResponse response = request.post("https://example.com/api/uploadTeamList", RequestOptions.create().setMultipart( FormData.create().set("fileField", file))); // Or you can pass the file content directly as FilePayload object: FilePayload filePayload = new FilePayload("f.js", "text/javascript", "console.log(2022);".getBytes(StandardCharsets.UTF_8)); APIResponse response = request.post("https://example.com/api/uploadTeamList", RequestOptions.create().setMultipart( FormData.create().set("fileField", filePayload)));- Parameters:
url- Target URL.params- Optional request parameters.- Since:
- v1.16
-
put
Sends HTTP(S) PUT request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.- Parameters:
url- Target URL.- Since:
- v1.16
-
put
Sends HTTP(S) PUT request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.- Parameters:
url- Target URL.params- Optional request parameters.- Since:
- v1.16
-
storageState
Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.- Since:
- v1.16
-
storageState
Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.- Since:
- v1.16
-