Skip to main content
Version: 1.22

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([options]).

Cookie management

APIRequestContext retuned 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 shoud create a new APIRequestContext by calling APIRequest.newContext([options]). Such APIRequestContext object will have its own isolated cookie storage.

APIRequestContext.delete(url[, options])

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.

APIRequestContext.dispose()

All responses returned by APIRequestContext.get(url[, options]) and similar methods are stored in the memory, so that you can later call APIResponse.body(). This method discards all stored responses, and makes APIResponse.body() throw "Response disposed" error.

APIRequestContext.fetch(urlOrRequest[, options])

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.

APIRequestContext.get(url[, options])

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.

APIRequestContext.head(url[, options])

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.

APIRequestContext.patch(url[, options])

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.

APIRequestContext.post(url[, options])

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.

APIRequestContext.put(url[, options])

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.

APIRequestContext.storageState([options])

  • options <ApiRequestContext.StorageStateOptions>
    • setPath <Path> The file path to save the storage state to. If path 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: <String>#

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