Interface APIRequestContext
-
public interface APIRequestContextThis API is used for the Web API testing. You can use it to trigger API endpoints, configure micro-services, prepare environment or the service to your e2e test.Each Playwright browser context has associated with it
APIRequestContextinstance which shares cookie storage with the browser context and can be accessed viaBrowserContext.request()orPage.request(). It is also possible to create a new APIRequestContext instance manually by callingAPIRequest.newContext().Cookie management
APIRequestContextreturned byBrowserContext.request()andPage.request()shares cookie storage with the correspondingBrowserContext. Each API request will haveCookieheader populated with the values from the browser context. If the API response containsSet-Cookieheader it will automatically updateBrowserContextcookies 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
APIRequestContextby callingAPIRequest.newContext(). SuchAPIRequestContextobject will have its own isolated cookie storage.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classAPIRequestContext.DisposeOptionsstatic classAPIRequestContext.StorageStateOptions
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default APIResponsedelete(String url)Sends HTTP(S) DELETE request and returns its response.APIResponsedelete(String url, RequestOptions params)Sends HTTP(S) DELETE request and returns its response.default voiddispose()All responses returned byAPIRequestContext.get()and similar methods are stored in the memory, so that you can later callAPIResponse.body().This method discards all its resources, calling any method on disposedAPIRequestContextwill throw an exception.voiddispose(APIRequestContext.DisposeOptions options)All responses returned byAPIRequestContext.get()and similar methods are stored in the memory, so that you can later callAPIResponse.body().This method discards all its resources, calling any method on disposedAPIRequestContextwill throw an exception.default APIResponsefetch(Request urlOrRequest)Sends HTTP(S) request and returns its response.APIResponsefetch(Request urlOrRequest, RequestOptions params)Sends HTTP(S) request and returns its response.default APIResponsefetch(String urlOrRequest)Sends HTTP(S) request and returns its response.APIResponsefetch(String urlOrRequest, RequestOptions params)Sends HTTP(S) request and returns its response.default APIResponseget(String url)Sends HTTP(S) GET request and returns its response.APIResponseget(String url, RequestOptions params)Sends HTTP(S) GET request and returns its response.default APIResponsehead(String url)Sends HTTP(S) HEAD request and returns its response.APIResponsehead(String url, RequestOptions params)Sends HTTP(S) HEAD request and returns its response.default APIResponsepatch(String url)Sends HTTP(S) PATCH request and returns its response.APIResponsepatch(String url, RequestOptions params)Sends HTTP(S) PATCH request and returns its response.default APIResponsepost(String url)Sends HTTP(S) POST request and returns its response.APIResponsepost(String url, RequestOptions params)Sends HTTP(S) POST request and returns its response.default APIResponseput(String url)Sends HTTP(S) PUT request and returns its response.APIResponseput(String url, RequestOptions params)Sends HTTP(S) PUT request and returns its response.default StringstorageState()Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.StringstorageState(APIRequestContext.StorageStateOptions options)Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.
-
-
-
Method Detail
-
delete
default APIResponse delete(String url)
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
APIResponse delete(String url, RequestOptions params)
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
default 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 its resources, calling any method on disposedAPIRequestContextwill throw an exception.- Since:
- v1.16
-
dispose
void dispose(APIRequestContext.DisposeOptions options)
All responses returned byAPIRequestContext.get()and similar methods are stored in the memory, so that you can later callAPIResponse.body().This method discards all its resources, calling any method on disposedAPIRequestContextwill throw an exception.- Since:
- v1.16
-
fetch
default APIResponse fetch(String urlOrRequest)
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.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.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 upload them as form fields with
multipart/form-dataencoding. UseFormDatato construct request body and pass it to the request asmultipartparameter:// 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/uploadScript", 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
APIResponse fetch(String urlOrRequest, RequestOptions params)
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.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.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 upload them as form fields with
multipart/form-dataencoding. UseFormDatato construct request body and pass it to the request asmultipartparameter:// 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/uploadScript", 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
default APIResponse fetch(Request urlOrRequest)
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.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.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 upload them as form fields with
multipart/form-dataencoding. UseFormDatato construct request body and pass it to the request asmultipartparameter:// 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/uploadScript", 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
APIResponse fetch(Request urlOrRequest, RequestOptions params)
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.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.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 upload them as form fields with
multipart/form-dataencoding. UseFormDatato construct request body and pass it to the request asmultipartparameter:// 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/uploadScript", 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
default APIResponse get(String url)
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
APIResponse get(String url, RequestOptions params)
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
default APIResponse head(String url)
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
APIResponse head(String url, RequestOptions params)
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
default APIResponse patch(String url)
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
APIResponse patch(String url, RequestOptions params)
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
default APIResponse post(String url)
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. UseFormDatato construct request body and pass it to the request asmultipartparameter:// 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 filePayload1 = new FilePayload("f1.js", "text/javascript", "console.log(2022);".getBytes(StandardCharsets.UTF_8)); APIResponse response = request.post("https://example.com/api/uploadScript", RequestOptions.create().setMultipart( FormData.create().set("fileField", filePayload)));- Parameters:
url- Target URL.- Since:
- v1.16
-
post
APIResponse post(String url, RequestOptions params)
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. UseFormDatato construct request body and pass it to the request asmultipartparameter:// 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 filePayload1 = new FilePayload("f1.js", "text/javascript", "console.log(2022);".getBytes(StandardCharsets.UTF_8)); APIResponse response = request.post("https://example.com/api/uploadScript", RequestOptions.create().setMultipart( FormData.create().set("fileField", filePayload)));- Parameters:
url- Target URL.params- Optional request parameters.- Since:
- v1.16
-
put
default APIResponse put(String url)
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
APIResponse put(String url, RequestOptions params)
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
default String 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
String storageState(APIRequestContext.StorageStateOptions options)
Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.- Since:
- v1.16
-
-