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

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([options]). Such APIRequestContext object will have its own isolated cookie storage.

apiRequestContext.delete(url[, options])

Added in: v1.16
  • url <string> Target URL.#
  • options? <Object>
    • data? <string|Buffer|Serializable> Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise the content-type header will be set to application/octet-stream if not explicitly set. Added in: v1.17#
    • failOnStatusCode? <boolean> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.#
    • form? <Object<string, string|number|boolean>> Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided. Added in: v1.17#
    • headers? <Object<string, string>> Allows to set HTTP headers.#
    • ignoreHTTPSErrors? <boolean> Whether to ignore HTTPS errors when sending network requests. Defaults to false.#
    • maxRedirects? <number> Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects. Added in: v1.26#
    • multipart? <Object<string, string|number|boolean|[ReadStream]|Object>> Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content. Added in: v1.17#
    • params? <Object<string, string|number|boolean>> Query parameters to be sent with the URL.#
    • timeout? <number> Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#
  • returns: <Promise<APIResponse>>#

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()

Added in: v1.16

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])

Added in: v1.16
  • urlOrRequest <string|Request> Target URL or Request to get all parameters from.#
  • options? <Object>
    • data? <string|Buffer|Serializable> Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise the content-type header will be set to application/octet-stream if not explicitly set.#
    • failOnStatusCode? <boolean> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.#
    • form? <Object<string, string|number|boolean>> Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.#
    • headers? <Object<string, string>> Allows to set HTTP headers.#
    • ignoreHTTPSErrors? <boolean> Whether to ignore HTTPS errors when sending network requests. Defaults to false.#
    • maxRedirects? <number> Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects. Added in: v1.26#
    • method? <string> If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used.#
    • multipart? <Object<string, string|number|boolean|[ReadStream]|Object>> Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.#
    • params? <Object<string, string|number|boolean>> Query parameters to be sent with the URL.#
    • timeout? <number> Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#
  • returns: <Promise<APIResponse>>#

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:

await request.fetch('https://example.com/api/createBook', {
method: 'post',
data: {
title: 'Book Title',
author: 'John Doe',
}
});

The common way to send file(s) in the body of a request is to encode it as form fields with multipart/form-data encoding. You can achieve that with Playwright API like this:

// Open file as a stream and pass it to the request:
const stream = fs.createReadStream('team.csv');
await request.fetch('https://example.com/api/uploadTeamList', {
method: 'post',
multipart: {
fileField: stream
}
});

// Or you can pass the file content directly as an object:
await request.fetch('https://example.com/api/uploadScript', {
method: 'post',
multipart: {
fileField: {
name: 'f.js',
mimeType: 'text/javascript',
buffer: Buffer.from('console.log(2022);')
}
}
});

apiRequestContext.get(url[, options])

Added in: v1.16
  • url <string> Target URL.#
  • options? <Object>
    • data? <string|Buffer|Serializable> Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise the content-type header will be set to application/octet-stream if not explicitly set. Added in: v1.26#
    • failOnStatusCode? <boolean> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.#
    • form? <Object<string, string|number|boolean>> Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided. Added in: v1.26#
    • headers? <Object<string, string>> Allows to set HTTP headers.#
    • ignoreHTTPSErrors? <boolean> Whether to ignore HTTPS errors when sending network requests. Defaults to false.#
    • maxRedirects? <number> Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects. Added in: v1.26#
    • multipart? <Object<string, string|number|boolean|[ReadStream]|Object>> Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content. Added in: v1.26#
    • params? <Object<string, string|number|boolean>> Query parameters to be sent with the URL.#
    • timeout? <number> Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#
  • returns: <Promise<APIResponse>>#

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.

Request parameters can be configured with params option, they will be serialized into the URL search parameters:

await request.get('https://example.com/api/getText', {
params: {
'isbn': '1234',
'page': 23,
}
});

apiRequestContext.head(url[, options])

Added in: v1.16
  • url <string> Target URL.#
  • options? <Object>
    • data? <string|Buffer|Serializable> Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise the content-type header will be set to application/octet-stream if not explicitly set. Added in: v1.26#
    • failOnStatusCode? <boolean> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.#
    • form? <Object<string, string|number|boolean>> Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided. Added in: v1.26#
    • headers? <Object<string, string>> Allows to set HTTP headers.#
    • ignoreHTTPSErrors? <boolean> Whether to ignore HTTPS errors when sending network requests. Defaults to false.#
    • maxRedirects? <number> Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects. Added in: v1.26#
    • multipart? <Object<string, string|number|boolean|[ReadStream]|Object>> Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content. Added in: v1.26#
    • params? <Object<string, string|number|boolean>> Query parameters to be sent with the URL.#
    • timeout? <number> Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#
  • returns: <Promise<APIResponse>>#

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])

Added in: v1.16
  • url <string> Target URL.#
  • options? <Object>
    • data? <string|Buffer|Serializable> Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise the content-type header will be set to application/octet-stream if not explicitly set.#
    • failOnStatusCode? <boolean> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.#
    • form? <Object<string, string|number|boolean>> Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.#
    • headers? <Object<string, string>> Allows to set HTTP headers.#
    • ignoreHTTPSErrors? <boolean> Whether to ignore HTTPS errors when sending network requests. Defaults to false.#
    • maxRedirects? <number> Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects. Added in: v1.26#
    • multipart? <Object<string, string|number|boolean|[ReadStream]|Object>> Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.#
    • params? <Object<string, string|number|boolean>> Query parameters to be sent with the URL.#
    • timeout? <number> Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#
  • returns: <Promise<APIResponse>>#

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])

Added in: v1.16
  • url <string> Target URL.#
  • options? <Object>
    • data? <string|Buffer|Serializable> Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise the content-type header will be set to application/octet-stream if not explicitly set.#
    • failOnStatusCode? <boolean> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.#
    • form? <Object<string, string|number|boolean>> Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.#
    • headers? <Object<string, string>> Allows to set HTTP headers.#
    • ignoreHTTPSErrors? <boolean> Whether to ignore HTTPS errors when sending network requests. Defaults to false.#
    • maxRedirects? <number> Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects. Added in: v1.26#
    • multipart? <Object<string, string|number|boolean|[ReadStream]|Object>> Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.#
    • params? <Object<string, string|number|boolean>> Query parameters to be sent with the URL.#
    • timeout? <number> Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#
  • returns: <Promise<APIResponse>>#

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.

JSON objects can be passed directly to the request:

await request.post('https://example.com/api/createBook', {
data: {
title: 'Book Title',
author: 'John Doe',
}
});

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):

await request.post('https://example.com/api/findBook', {
form: {
title: 'Book Title',
author: '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. You can achieve that with Playwright API like this:

// Open file as a stream and pass it to the request:
const stream = fs.createReadStream('team.csv');
await request.post('https://example.com/api/uploadTeamList', {
multipart: {
fileField: stream
}
});

// Or you can pass the file content directly as an object:
await request.post('https://example.com/api/uploadScript', {
multipart: {
fileField: {
name: 'f.js',
mimeType: 'text/javascript',
buffer: Buffer.from('console.log(2022);')
}
}
});

apiRequestContext.put(url[, options])

Added in: v1.16
  • url <string> Target URL.#
  • options? <Object>
    • data? <string|Buffer|Serializable> Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type header will be set to application/json if not explicitly set. Otherwise the content-type header will be set to application/octet-stream if not explicitly set.#
    • failOnStatusCode? <boolean> Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.#
    • form? <Object<string, string|number|boolean>> Provides an object that will be serialized as html form using application/x-www-form-urlencoded encoding and sent as this request body. If this parameter is specified content-type header will be set to application/x-www-form-urlencoded unless explicitly provided.#
    • headers? <Object<string, string>> Allows to set HTTP headers.#
    • ignoreHTTPSErrors? <boolean> Whether to ignore HTTPS errors when sending network requests. Defaults to false.#
    • maxRedirects? <number> Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects. Added in: v1.26#
    • multipart? <Object<string, string|number|boolean|[ReadStream]|Object>> Provides an object that will be serialized as html form using multipart/form-data encoding and sent as this request body. If this parameter is specified content-type header will be set to multipart/form-data unless explicitly provided. File values can be passed either as fs.ReadStream or as file-like object containing file name, mime-type and its content.#
    • params? <Object<string, string|number|boolean>> Query parameters to be sent with the URL.#
    • timeout? <number> Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.#
  • returns: <Promise<APIResponse>>#

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])

Added in: v1.16

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