Skip to main content

APIRequestContext

This 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 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.


Methods

delete

Added in: v1.16 apiRequestContext.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.

Usage

APIRequestContext.delete(url);
APIRequestContext.delete(url, options);

Arguments

Returns


dispose

Added in: v1.16 apiRequestContext.dispose

All responses returned by APIRequestContext.get() and similar methods are stored in the memory, so that you can later call APIResponse.body().This method discards all its resources, calling any method on disposed APIRequestContext will throw an exception.

Usage

APIRequestContext.dispose();
APIRequestContext.dispose(options);

Arguments

  • options ApiRequestContext.DisposeOptions (optional)
    • setReason String (optional) Added in: v1.45#

      The reason to be reported to the operations interrupted by the context disposal.

Returns


fetch

Added in: v1.16 apiRequestContext.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.

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-data encoding, by specifiying the multipart parameter:

// 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)));

Arguments

  • urlOrRequest String | Request#

    Target URL or Request to get all parameters from.

  • options RequestOptions (optional) Added in: v1.18#

    Optional request parameters.

Returns


get

Added in: v1.16 apiRequestContext.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 params option, they will be serialized into the URL search parameters:

request.get("https://example.com/api/getText", RequestOptions.create()
.setQueryParam("isbn", "1234")
.setQueryParam("page", 23));

Arguments

Returns


head

Added in: v1.16 apiRequestContext.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.

Usage

APIRequestContext.head(url);
APIRequestContext.head(url, options);

Arguments

Returns


patch

Added in: v1.16 apiRequestContext.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.

Usage

APIRequestContext.patch(url);
APIRequestContext.patch(url, options);

Arguments

Returns


post

Added in: v1.16 apiRequestContext.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 form option. Its value will be encoded into the request body with application/x-www-form-urlencoded encoding (see below how to use multipart/form-data form 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-data encoding. Use FormData to construct request body and pass it to the request as multipart parameter:

// 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)));

Arguments

Returns


put

Added in: v1.16 apiRequestContext.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.

Usage

APIRequestContext.put(url);
APIRequestContext.put(url, options);

Arguments

Returns


storageState

Added in: v1.16 apiRequestContext.storageState

Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.

Usage

APIRequestContext.storageState();
APIRequestContext.storageState(options);

Arguments

  • options ApiRequestContext.StorageStateOptions (optional)
    • setPath Path (optional)#

      The file path to save the storage state to. If setPath is a relative path, then it is resolved relative to current working directory. If no path is provided, storage state is still returned, but won't be saved to the disk.

Returns