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 an associated APIRequestContext, accessible via
Playwright.BrowserContext.request/1 or Playwright.Page.request/1 (these return the
same instance — Playwright.Page.request/1 is a shortcut for Playwright.BrowserContext.request/1 of Playwright.Page.context/1).
You can also create a standalone, isolated instance with Playwright.APIRequest.new_context/2.
Cookie management
The APIRequestContext returned by Playwright.BrowserContext.request/1 and
Playwright.Page.request/1 uses the same cookie jar as its BrowserContext:
If you want API requests that do not share cookies with the browser, create an
isolated context via Playwright.APIRequest.new_context/2. Such APIRequestContext
object will have its own isolated cookie storage.
Since Playwright 1.16.
The examples alias the modules they use (alias Playwright.{Locator, Page}).
Summary
Types
Options of clear_cookies/2, see its docs.
Options of delete/3, see its docs.
Options of dispose/2, see its docs.
Options of fetch/3, see its docs.
Options of get/3, see its docs.
Options of head/3, see its docs.
Options of patch/3, see its docs.
Options of post/3, see its docs.
Options of put/3, see its docs.
Options of storage_state/2, see its docs.
A APIRequestContext handle. Compare handles with ==, their fields are internal.
Functions
Adds cookies into this request context. They will be sent with matching subsequent requests. For Playwright.BrowserContext.request/1
and Playwright.Page.request/1, this is equivalent to calling Playwright.BrowserContext.add_cookies/2 on the corresponding browser context.
Like add_cookies/2, but returns the value and raises on error.
Removes cookies from this request context. Accepts optional filter. For Playwright.BrowserContext.request/1
and Playwright.Page.request/1, this is equivalent to calling Playwright.BrowserContext.clear_cookies/2 on the corresponding browser context.
Like clear_cookies/2, but returns the value and raises on error.
If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs
are returned. For Playwright.BrowserContext.request/1 and Playwright.Page.request/1, this is equivalent to calling
Playwright.BrowserContext.cookies/2 on the corresponding browser context.
Like cookies/2, but returns the value and raises on error.
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.
Like delete/3, but returns the value and raises on error.
All responses returned by Playwright.APIRequestContext.get/3 and similar methods are stored in the memory, so that you can later call Playwright.APIResponse.body/1.This method discards all its resources, calling any method on disposed APIRequestContext will fail.
Like dispose/2, but returns the value and raises on error.
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.
Like fetch/3, but returns the value and raises on error.
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.
Like get/3, but returns the value and raises on error.
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.
Like head/3, but returns the value and raises on error.
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.
Like patch/3, but returns the value and raises on error.
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.
Like post/3, but returns the value and raises on error.
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.
Like put/3, but returns the value and raises on error.
Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.
Like storage_state/2, but returns the value and raises on error.
Tracing recorder for requests made through this API request context.
Types
@type clear_cookies_options() :: [ domain: String.t() | Regex.t() | Playwright.RegExp.t(), name: String.t() | Regex.t() | Playwright.RegExp.t(), path: String.t() | Regex.t() | Playwright.RegExp.t() ] | %{ optional(:domain) => String.t() | Regex.t() | Playwright.RegExp.t(), optional(:name) => String.t() | Regex.t() | Playwright.RegExp.t(), optional(:path) => String.t() | Regex.t() | Playwright.RegExp.t() }
Options of clear_cookies/2, see its docs.
@type delete_options() :: [ data: String.t() | {:binary, binary()} | term(), fail_on_status_code: boolean(), form: %{optional(String.t()) => String.t() | number() | boolean()} | term(), headers: %{optional(String.t()) => String.t()}, ignore_https_errors: boolean(), max_redirects: integer(), max_retries: integer(), multipart: term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, params: %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), signal: Playwright.AbortSignal.t(), timeout: number() | :infinity ] | %{ optional(:data) => String.t() | {:binary, binary()} | term(), optional(:fail_on_status_code) => boolean(), optional(:form) => %{optional(String.t()) => String.t() | number() | boolean()} | term(), optional(:headers) => %{optional(String.t()) => String.t()}, optional(:ignore_https_errors) => boolean(), optional(:max_redirects) => integer(), optional(:max_retries) => integer(), optional(:multipart) => term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, optional(:params) => %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), optional(:signal) => Playwright.AbortSignal.t(), optional(:timeout) => number() | :infinity }
Options of delete/3, see its docs.
Options of dispose/2, see its docs.
@type fetch_options() :: [ data: String.t() | {:binary, binary()} | term(), fail_on_status_code: boolean(), form: %{optional(String.t()) => String.t() | number() | boolean()} | term(), headers: %{optional(String.t()) => String.t()}, ignore_https_errors: boolean(), max_redirects: integer(), max_retries: integer(), method: String.t(), multipart: term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, params: %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), signal: Playwright.AbortSignal.t(), timeout: number() | :infinity ] | %{ optional(:data) => String.t() | {:binary, binary()} | term(), optional(:fail_on_status_code) => boolean(), optional(:form) => %{optional(String.t()) => String.t() | number() | boolean()} | term(), optional(:headers) => %{optional(String.t()) => String.t()}, optional(:ignore_https_errors) => boolean(), optional(:max_redirects) => integer(), optional(:max_retries) => integer(), optional(:method) => String.t(), optional(:multipart) => term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, optional(:params) => %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), optional(:signal) => Playwright.AbortSignal.t(), optional(:timeout) => number() | :infinity }
Options of fetch/3, see its docs.
@type get_options() :: [ data: String.t() | {:binary, binary()} | term(), fail_on_status_code: boolean(), form: %{optional(String.t()) => String.t() | number() | boolean()} | term(), headers: %{optional(String.t()) => String.t()}, ignore_https_errors: boolean(), max_redirects: integer(), max_retries: integer(), multipart: term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, params: %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), signal: Playwright.AbortSignal.t(), timeout: number() | :infinity ] | %{ optional(:data) => String.t() | {:binary, binary()} | term(), optional(:fail_on_status_code) => boolean(), optional(:form) => %{optional(String.t()) => String.t() | number() | boolean()} | term(), optional(:headers) => %{optional(String.t()) => String.t()}, optional(:ignore_https_errors) => boolean(), optional(:max_redirects) => integer(), optional(:max_retries) => integer(), optional(:multipart) => term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, optional(:params) => %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), optional(:signal) => Playwright.AbortSignal.t(), optional(:timeout) => number() | :infinity }
Options of get/3, see its docs.
@type head_options() :: [ data: String.t() | {:binary, binary()} | term(), fail_on_status_code: boolean(), form: %{optional(String.t()) => String.t() | number() | boolean()} | term(), headers: %{optional(String.t()) => String.t()}, ignore_https_errors: boolean(), max_redirects: integer(), max_retries: integer(), multipart: term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, params: %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), signal: Playwright.AbortSignal.t(), timeout: number() | :infinity ] | %{ optional(:data) => String.t() | {:binary, binary()} | term(), optional(:fail_on_status_code) => boolean(), optional(:form) => %{optional(String.t()) => String.t() | number() | boolean()} | term(), optional(:headers) => %{optional(String.t()) => String.t()}, optional(:ignore_https_errors) => boolean(), optional(:max_redirects) => integer(), optional(:max_retries) => integer(), optional(:multipart) => term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, optional(:params) => %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), optional(:signal) => Playwright.AbortSignal.t(), optional(:timeout) => number() | :infinity }
Options of head/3, see its docs.
@type patch_options() :: [ data: String.t() | {:binary, binary()} | term(), fail_on_status_code: boolean(), form: %{optional(String.t()) => String.t() | number() | boolean()} | term(), headers: %{optional(String.t()) => String.t()}, ignore_https_errors: boolean(), max_redirects: integer(), max_retries: integer(), multipart: term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, params: %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), signal: Playwright.AbortSignal.t(), timeout: number() | :infinity ] | %{ optional(:data) => String.t() | {:binary, binary()} | term(), optional(:fail_on_status_code) => boolean(), optional(:form) => %{optional(String.t()) => String.t() | number() | boolean()} | term(), optional(:headers) => %{optional(String.t()) => String.t()}, optional(:ignore_https_errors) => boolean(), optional(:max_redirects) => integer(), optional(:max_retries) => integer(), optional(:multipart) => term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, optional(:params) => %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), optional(:signal) => Playwright.AbortSignal.t(), optional(:timeout) => number() | :infinity }
Options of patch/3, see its docs.
@type post_options() :: [ data: String.t() | {:binary, binary()} | term(), fail_on_status_code: boolean(), form: %{optional(String.t()) => String.t() | number() | boolean()} | term(), headers: %{optional(String.t()) => String.t()}, ignore_https_errors: boolean(), max_redirects: integer(), max_retries: integer(), multipart: term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, params: %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), signal: Playwright.AbortSignal.t(), timeout: number() | :infinity ] | %{ optional(:data) => String.t() | {:binary, binary()} | term(), optional(:fail_on_status_code) => boolean(), optional(:form) => %{optional(String.t()) => String.t() | number() | boolean()} | term(), optional(:headers) => %{optional(String.t()) => String.t()}, optional(:ignore_https_errors) => boolean(), optional(:max_redirects) => integer(), optional(:max_retries) => integer(), optional(:multipart) => term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, optional(:params) => %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), optional(:signal) => Playwright.AbortSignal.t(), optional(:timeout) => number() | :infinity }
Options of post/3, see its docs.
@type put_options() :: [ data: String.t() | {:binary, binary()} | term(), fail_on_status_code: boolean(), form: %{optional(String.t()) => String.t() | number() | boolean()} | term(), headers: %{optional(String.t()) => String.t()}, ignore_https_errors: boolean(), max_redirects: integer(), max_retries: integer(), multipart: term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, params: %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), signal: Playwright.AbortSignal.t(), timeout: number() | :infinity ] | %{ optional(:data) => String.t() | {:binary, binary()} | term(), optional(:fail_on_status_code) => boolean(), optional(:form) => %{optional(String.t()) => String.t() | number() | boolean()} | term(), optional(:headers) => %{optional(String.t()) => String.t()}, optional(:ignore_https_errors) => boolean(), optional(:max_redirects) => integer(), optional(:max_retries) => integer(), optional(:multipart) => term() | %{ optional(String.t()) => String.t() | number() | boolean() | term() | %{name: String.t(), mimeType: String.t(), buffer: binary()} }, optional(:params) => %{optional(String.t()) => String.t() | number() | boolean()} | term() | String.t(), optional(:signal) => Playwright.AbortSignal.t(), optional(:timeout) => number() | :infinity }
Options of put/3, see its docs.
@type storage_state_options() :: [indexed_db: boolean(), opfs: boolean(), path: String.t()] | %{ optional(:indexed_db) => boolean(), optional(:opfs) => boolean(), optional(:path) => String.t() }
Options of storage_state/2, see its docs.
@type t() :: %Playwright.APIRequestContext{ conn: term(), guid: term(), initializer: term(), table: term(), type: term() }
A APIRequestContext handle. Compare handles with ==, their fields are internal.
Functions
@spec add_cookies(t(), [map()]) :: :ok | {:error, Playwright.error()}
Adds cookies into this request context. They will be sent with matching subsequent requests. For Playwright.BrowserContext.request/1
and Playwright.Page.request/1, this is equivalent to calling Playwright.BrowserContext.add_cookies/2 on the corresponding browser context.
Usage
APIRequestContext.add_cookies!(request, [cookie_object1, cookie_object2])Since Playwright 1.64.
Arguments
cookies:name:value:url- Eitherurlor bothdomainandpathare required. Optional.:domain- For the cookie to apply to all subdomains as well, prefix domain with a dot, like this: ".example.com". Eitherurlor bothdomainandpathare required. Optional.:path- Eitherurlor bothdomainandpathare required. Optional.:expires- Unix time in seconds. Optional.:httpOnly- Optional.:secure- Optional.:sameSite- Optional.:partitionKey- For partitioned third-party cookies (aka CHIPS), the partition key. Optional.
Like add_cookies/2, but returns the value and raises on error.
@spec clear_cookies(t(), clear_cookies_options()) :: :ok | {:error, Playwright.error()}
Removes cookies from this request context. Accepts optional filter. For Playwright.BrowserContext.request/1
and Playwright.Page.request/1, this is equivalent to calling Playwright.BrowserContext.clear_cookies/2 on the corresponding browser context.
Usage
APIRequestContext.clear_cookies!(request)
APIRequestContext.clear_cookies!(request, name: "session-id")
APIRequestContext.clear_cookies!(request, domain: "my-origin.com")
APIRequestContext.clear_cookies!(request, domain: ~r/.*my-origin\.com/)
APIRequestContext.clear_cookies!(request, path: "/api/v1")
APIRequestContext.clear_cookies!(request, name: "session-id", domain: "my-origin.com")Since Playwright 1.64.
Options
:domain(String.t/0|Regex.t/0|Playwright.RegExp.t/0) - Only removes cookies with the given domain.:name(String.t/0|Regex.t/0|Playwright.RegExp.t/0) - Only removes cookies with the given name.:path(String.t/0|Regex.t/0|Playwright.RegExp.t/0) - Only removes cookies with the given path.
@spec clear_cookies!(t(), clear_cookies_options()) :: :ok
Like clear_cookies/2, but returns the value and raises on error.
@spec cookies(t(), String.t() | [String.t()] | nil) :: {:ok, [ %{ :name => String.t(), :value => String.t(), :domain => String.t(), :path => String.t(), :expires => number(), :httpOnly => boolean(), :secure => boolean(), :sameSite => String.t(), optional(:partitionKey) => String.t() } ]} | {:error, Playwright.error()}
If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs
are returned. For Playwright.BrowserContext.request/1 and Playwright.Page.request/1, this is equivalent to calling
Playwright.BrowserContext.cookies/2 on the corresponding browser context.
Since Playwright 1.64.
Arguments
urls- Optional list of URLs.
@spec cookies!(t(), String.t() | [String.t()] | nil) :: [ %{ :name => String.t(), :value => String.t(), :domain => String.t(), :path => String.t(), :expires => number(), :httpOnly => boolean(), :secure => boolean(), :sameSite => String.t(), optional(:partitionKey) => String.t() } ]
Like cookies/2, but returns the value and raises on error.
@spec delete(t(), String.t(), delete_options()) :: {:ok, Playwright.APIResponse.t()} | {:error, Playwright.error()}
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.
Since Playwright 1.16.
Arguments
url- Target URL.
Options
:data(String.t/0|{:binary, binary()}|term/0) - Since Playwright 1.17. Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.:fail_on_status_code(boolean/0) - Whether to fail on response codes other than 2xx and 3xx. By default response object is returned for all status codes.:form(%{optional(String.t()) => String.t() | number() | boolean()}|term/0) - Since Playwright 1.17. Provides an object that will be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set toapplication/x-www-form-urlencodedunless explicitly provided. UseFormDatato send multiple values for the same field.:headers(%{optional(String.t()) => String.t()}) - Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.:ignore_https_errors(boolean/0) - Whether to ignore HTTPS errors when sending network requests. Defaults tofalse.:max_redirects(integer/0) - Since Playwright 1.26. Maximum number of request redirects that will be followed automatically. The call fails if the number is exceeded. Defaults to20. Pass0to not follow redirects.:max_retries(integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently onlyECONNRESETerror is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to0- no retries.:multipart(term/0|map/0) - Since Playwright 1.17. Provides an object that will be serialized as html form usingmultipart/form-dataencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set tomultipart/form-dataunless explicitly provided. File values can be passed either asfs.ReadStreamor as file-like object containing file name, mime-type and its content.:name- File name:mimeType- File type:buffer- File content
:params(%{optional(String.t()) => String.t() | number() | boolean()}|term/0|String.t/0) - Query parameters to be sent with the URL.:signal(Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using anAbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed usingPlaywright.BrowserContext.set_default_timeout/2orPlaywright.Page.set_default_timeout/2; passtimeout: 0to disable the timeout entirely.In Elixir, a
Playwright.AbortSignal(Playwright.AbortController.signal/1).:timeout(number/0|:infinity) - Request timeout in milliseconds. Defaults to30000(30 seconds). Pass0to disable timeout.In Elixir,
:infinityis the same as0: no timeout.
@spec delete!(t(), String.t(), delete_options()) :: Playwright.APIResponse.t()
Like delete/3, but returns the value and raises on error.
@spec dispose(t(), dispose_options()) :: :ok | {:error, Playwright.error()}
All responses returned by Playwright.APIRequestContext.get/3 and similar methods are stored in the memory, so that you can later call Playwright.APIResponse.body/1.This method discards all its resources, calling any method on disposed APIRequestContext will fail.
Since Playwright 1.16.
Options
:reason(String.t/0) - Since Playwright 1.45. The reason to be reported to the operations interrupted by the context disposal.
@spec dispose!(t(), dispose_options()) :: :ok
Like dispose/2, but returns the value and raises on error.
@spec fetch(t(), String.t() | Playwright.Request.t(), fetch_options()) :: {:ok, Playwright.APIResponse.t()} | {:error, Playwright.error()}
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:
APIRequestContext.fetch!(request, "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 upload them as form fields with multipart/form-data encoding, by specifiying the :multipart parameter:
form = [
name: "John",
name: "Doe",
# Send two file fields with the same name.
file: %{name: "f1.js", mimeType: "text/javascript", buffer: "console.log(2024);"},
file: %{name: "f2.txt", mimeType: "text/plain", buffer: "hello"}
]
APIRequestContext.fetch!(request, "https://example.com/api/uploadForm", multipart: form)Since Playwright 1.16.
Arguments
url_or_request- Target URL or Request to get all parameters from.
Options
:data(String.t/0|{:binary, binary()}|term/0) - Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.:fail_on_status_code(boolean/0) - Whether to fail on response codes other than 2xx and 3xx. By default response object is returned for all status codes.:form(%{optional(String.t()) => String.t() | number() | boolean()}|term/0) - Provides an object that will be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set toapplication/x-www-form-urlencodedunless explicitly provided. UseFormDatato send multiple values for the same field.:headers(%{optional(String.t()) => String.t()}) - Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.:ignore_https_errors(boolean/0) - Whether to ignore HTTPS errors when sending network requests. Defaults tofalse.:max_redirects(integer/0) - Since Playwright 1.26. Maximum number of request redirects that will be followed automatically. The call fails if the number is exceeded. Defaults to20. Pass0to not follow redirects.:max_retries(integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently onlyECONNRESETerror is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to0- no retries.:method(String.t/0) - If set changes the fetch method (e.g. PUT or POST). If not specified, GET method is used.:multipart(term/0|map/0) - Provides an object that will be serialized as html form usingmultipart/form-dataencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set tomultipart/form-dataunless explicitly provided. File values can be passed either asfs.ReadStreamor as file-like object containing file name, mime-type and its content.:name- File name:mimeType- File type:buffer- File content
:params(%{optional(String.t()) => String.t() | number() | boolean()}|term/0|String.t/0) - Query parameters to be sent with the URL.:signal(Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using anAbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed usingPlaywright.BrowserContext.set_default_timeout/2orPlaywright.Page.set_default_timeout/2; passtimeout: 0to disable the timeout entirely.In Elixir, a
Playwright.AbortSignal(Playwright.AbortController.signal/1).:timeout(number/0|:infinity) - Request timeout in milliseconds. Defaults to30000(30 seconds). Pass0to disable timeout.In Elixir,
:infinityis the same as0: no timeout.
@spec fetch!(t(), String.t() | Playwright.Request.t(), fetch_options()) :: Playwright.APIResponse.t()
Like fetch/3, but returns the value and raises on error.
@spec get(t(), String.t(), get_options()) :: {:ok, Playwright.APIResponse.t()} | {:error, Playwright.error()}
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:
# Passing params as map
APIRequestContext.get!(request, "https://example.com/api/getText", params: %{"isbn" => "1234", "page" => 23})
# Passing params as keyword list (URLSearchParams, names may repeat)
search_params = [isbn: "1234", page: 23, page: 24]
APIRequestContext.get!(request, "https://example.com/api/getText", params: search_params)
# Passing params as string
query_string = "isbn=1234&page=23&page=24"
APIRequestContext.get!(request, "https://example.com/api/getText", params: query_string)Since Playwright 1.16.
Arguments
url- Target URL.
Options
:data(String.t/0|{:binary, binary()}|term/0) - Since Playwright 1.26. Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.:fail_on_status_code(boolean/0) - Whether to fail on response codes other than 2xx and 3xx. By default response object is returned for all status codes.:form(%{optional(String.t()) => String.t() | number() | boolean()}|term/0) - Since Playwright 1.26. Provides an object that will be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set toapplication/x-www-form-urlencodedunless explicitly provided. UseFormDatato send multiple values for the same field.:headers(%{optional(String.t()) => String.t()}) - Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.:ignore_https_errors(boolean/0) - Whether to ignore HTTPS errors when sending network requests. Defaults tofalse.:max_redirects(integer/0) - Since Playwright 1.26. Maximum number of request redirects that will be followed automatically. The call fails if the number is exceeded. Defaults to20. Pass0to not follow redirects.:max_retries(integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently onlyECONNRESETerror is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to0- no retries.:multipart(term/0|map/0) - Since Playwright 1.26. Provides an object that will be serialized as html form usingmultipart/form-dataencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set tomultipart/form-dataunless explicitly provided. File values can be passed either asfs.ReadStreamor as file-like object containing file name, mime-type and its content.:name- File name:mimeType- File type:buffer- File content
:params(%{optional(String.t()) => String.t() | number() | boolean()}|term/0|String.t/0) - Query parameters to be sent with the URL.:signal(Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using anAbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed usingPlaywright.BrowserContext.set_default_timeout/2orPlaywright.Page.set_default_timeout/2; passtimeout: 0to disable the timeout entirely.In Elixir, a
Playwright.AbortSignal(Playwright.AbortController.signal/1).:timeout(number/0|:infinity) - Request timeout in milliseconds. Defaults to30000(30 seconds). Pass0to disable timeout.In Elixir,
:infinityis the same as0: no timeout.
@spec get!(t(), String.t(), get_options()) :: Playwright.APIResponse.t()
Like get/3, but returns the value and raises on error.
@spec head(t(), String.t(), head_options()) :: {:ok, Playwright.APIResponse.t()} | {:error, Playwright.error()}
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.
Since Playwright 1.16.
Arguments
url- Target URL.
Options
:data(String.t/0|{:binary, binary()}|term/0) - Since Playwright 1.26. Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.:fail_on_status_code(boolean/0) - Whether to fail on response codes other than 2xx and 3xx. By default response object is returned for all status codes.:form(%{optional(String.t()) => String.t() | number() | boolean()}|term/0) - Since Playwright 1.26. Provides an object that will be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set toapplication/x-www-form-urlencodedunless explicitly provided. UseFormDatato send multiple values for the same field.:headers(%{optional(String.t()) => String.t()}) - Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.:ignore_https_errors(boolean/0) - Whether to ignore HTTPS errors when sending network requests. Defaults tofalse.:max_redirects(integer/0) - Since Playwright 1.26. Maximum number of request redirects that will be followed automatically. The call fails if the number is exceeded. Defaults to20. Pass0to not follow redirects.:max_retries(integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently onlyECONNRESETerror is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to0- no retries.:multipart(term/0|map/0) - Since Playwright 1.26. Provides an object that will be serialized as html form usingmultipart/form-dataencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set tomultipart/form-dataunless explicitly provided. File values can be passed either asfs.ReadStreamor as file-like object containing file name, mime-type and its content.:name- File name:mimeType- File type:buffer- File content
:params(%{optional(String.t()) => String.t() | number() | boolean()}|term/0|String.t/0) - Query parameters to be sent with the URL.:signal(Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using anAbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed usingPlaywright.BrowserContext.set_default_timeout/2orPlaywright.Page.set_default_timeout/2; passtimeout: 0to disable the timeout entirely.In Elixir, a
Playwright.AbortSignal(Playwright.AbortController.signal/1).:timeout(number/0|:infinity) - Request timeout in milliseconds. Defaults to30000(30 seconds). Pass0to disable timeout.In Elixir,
:infinityis the same as0: no timeout.
@spec head!(t(), String.t(), head_options()) :: Playwright.APIResponse.t()
Like head/3, but returns the value and raises on error.
@spec patch(t(), String.t(), patch_options()) :: {:ok, Playwright.APIResponse.t()} | {:error, Playwright.error()}
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.
Since Playwright 1.16.
Arguments
url- Target URL.
Options
:data(String.t/0|{:binary, binary()}|term/0) - Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.:fail_on_status_code(boolean/0) - Whether to fail on response codes other than 2xx and 3xx. By default response object is returned for all status codes.:form(%{optional(String.t()) => String.t() | number() | boolean()}|term/0) - Provides an object that will be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set toapplication/x-www-form-urlencodedunless explicitly provided. UseFormDatato send multiple values for the same field.:headers(%{optional(String.t()) => String.t()}) - Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.:ignore_https_errors(boolean/0) - Whether to ignore HTTPS errors when sending network requests. Defaults tofalse.:max_redirects(integer/0) - Since Playwright 1.26. Maximum number of request redirects that will be followed automatically. The call fails if the number is exceeded. Defaults to20. Pass0to not follow redirects.:max_retries(integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently onlyECONNRESETerror is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to0- no retries.:multipart(term/0|map/0) - Provides an object that will be serialized as html form usingmultipart/form-dataencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set tomultipart/form-dataunless explicitly provided. File values can be passed either asfs.ReadStreamor as file-like object containing file name, mime-type and its content.:name- File name:mimeType- File type:buffer- File content
:params(%{optional(String.t()) => String.t() | number() | boolean()}|term/0|String.t/0) - Query parameters to be sent with the URL.:signal(Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using anAbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed usingPlaywright.BrowserContext.set_default_timeout/2orPlaywright.Page.set_default_timeout/2; passtimeout: 0to disable the timeout entirely.In Elixir, a
Playwright.AbortSignal(Playwright.AbortController.signal/1).:timeout(number/0|:infinity) - Request timeout in milliseconds. Defaults to30000(30 seconds). Pass0to disable timeout.In Elixir,
:infinityis the same as0: no timeout.
@spec patch!(t(), String.t(), patch_options()) :: Playwright.APIResponse.t()
Like patch/3, but returns the value and raises on error.
@spec post(t(), String.t(), post_options()) :: {:ok, Playwright.APIResponse.t()} | {:error, Playwright.error()}
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:
APIRequestContext.post!(request, "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):
APIRequestContext.post!(request, "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. Use FormData to construct request body and pass it to the request as :multipart parameter:
form = [
name: "John",
name: "Doe",
# Send two file fields with the same name.
file: %{name: "f1.js", mimeType: "text/javascript", buffer: "console.log(2024);"},
file: %{name: "f2.txt", mimeType: "text/plain", buffer: "hello"}
]
APIRequestContext.post!(request, "https://example.com/api/uploadForm", multipart: form)Since Playwright 1.16.
Arguments
url- Target URL.
Options
:data(String.t/0|{:binary, binary()}|term/0) - Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.:fail_on_status_code(boolean/0) - Whether to fail on response codes other than 2xx and 3xx. By default response object is returned for all status codes.:form(%{optional(String.t()) => String.t() | number() | boolean()}|term/0) - Provides an object that will be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set toapplication/x-www-form-urlencodedunless explicitly provided. UseFormDatato send multiple values for the same field.:headers(%{optional(String.t()) => String.t()}) - Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.:ignore_https_errors(boolean/0) - Whether to ignore HTTPS errors when sending network requests. Defaults tofalse.:max_redirects(integer/0) - Since Playwright 1.26. Maximum number of request redirects that will be followed automatically. The call fails if the number is exceeded. Defaults to20. Pass0to not follow redirects.:max_retries(integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently onlyECONNRESETerror is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to0- no retries.:multipart(term/0|map/0) - Provides an object that will be serialized as html form usingmultipart/form-dataencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set tomultipart/form-dataunless explicitly provided. File values can be passed either asfs.ReadStreamor as file-like object containing file name, mime-type and its content.:name- File name:mimeType- File type:buffer- File content
:params(%{optional(String.t()) => String.t() | number() | boolean()}|term/0|String.t/0) - Query parameters to be sent with the URL.:signal(Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using anAbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed usingPlaywright.BrowserContext.set_default_timeout/2orPlaywright.Page.set_default_timeout/2; passtimeout: 0to disable the timeout entirely.In Elixir, a
Playwright.AbortSignal(Playwright.AbortController.signal/1).:timeout(number/0|:infinity) - Request timeout in milliseconds. Defaults to30000(30 seconds). Pass0to disable timeout.In Elixir,
:infinityis the same as0: no timeout.
@spec post!(t(), String.t(), post_options()) :: Playwright.APIResponse.t()
Like post/3, but returns the value and raises on error.
@spec put(t(), String.t(), put_options()) :: {:ok, Playwright.APIResponse.t()} | {:error, Playwright.error()}
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.
Since Playwright 1.16.
Arguments
url- Target URL.
Options
:data(String.t/0|{:binary, binary()}|term/0) - Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.:fail_on_status_code(boolean/0) - Whether to fail on response codes other than 2xx and 3xx. By default response object is returned for all status codes.:form(%{optional(String.t()) => String.t() | number() | boolean()}|term/0) - Provides an object that will be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set toapplication/x-www-form-urlencodedunless explicitly provided. UseFormDatato send multiple values for the same field.:headers(%{optional(String.t()) => String.t()}) - Allows to set HTTP headers. These headers will apply to the fetched request as well as any redirects initiated by it.:ignore_https_errors(boolean/0) - Whether to ignore HTTPS errors when sending network requests. Defaults tofalse.:max_redirects(integer/0) - Since Playwright 1.26. Maximum number of request redirects that will be followed automatically. The call fails if the number is exceeded. Defaults to20. Pass0to not follow redirects.:max_retries(integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently onlyECONNRESETerror is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to0- no retries.:multipart(term/0|map/0) - Provides an object that will be serialized as html form usingmultipart/form-dataencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set tomultipart/form-dataunless explicitly provided. File values can be passed either asfs.ReadStreamor as file-like object containing file name, mime-type and its content.:name- File name:mimeType- File type:buffer- File content
:params(%{optional(String.t()) => String.t() | number() | boolean()}|term/0|String.t/0) - Query parameters to be sent with the URL.:signal(Playwright.AbortSignal.t/0) - Since Playwright 1.62. Allows to cancel the operation using anAbortSignal. If the signal is aborted, the operation will be aborted and fail. Note that providing a signal does not disable the default timeout, which can be changed usingPlaywright.BrowserContext.set_default_timeout/2orPlaywright.Page.set_default_timeout/2; passtimeout: 0to disable the timeout entirely.In Elixir, a
Playwright.AbortSignal(Playwright.AbortController.signal/1).:timeout(number/0|:infinity) - Request timeout in milliseconds. Defaults to30000(30 seconds). Pass0to disable timeout.In Elixir,
:infinityis the same as0: no timeout.
@spec put!(t(), String.t(), put_options()) :: Playwright.APIResponse.t()
Like put/3, but returns the value and raises on error.
@spec storage_state(t(), storage_state_options()) :: {:ok, %{ :cookies => [ %{ :name => String.t(), :value => String.t(), :domain => String.t(), :path => String.t(), :expires => number(), :httpOnly => boolean(), :secure => boolean(), :sameSite => String.t(), optional(atom()) => term() } ], :origins => [ %{ :origin => String.t(), :localStorage => [ %{ :name => String.t(), :value => String.t(), optional(atom()) => term() } ], optional(atom()) => term() } ], optional(atom()) => term() }} | {:error, Playwright.error()}
Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.
Since Playwright 1.16.
Options
:indexed_db(boolean/0) - Since Playwright 1.51. Set totrueto include IndexedDB in the storage state snapshot.:opfs(boolean/0) - Since Playwright 1.63. Set totrueto include the origin private file system in the storage state snapshot.:path(String.t/0) - The file path to save the storage state to. If:pathis 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.
@spec storage_state!(t(), storage_state_options()) :: %{ :cookies => [ %{ :name => String.t(), :value => String.t(), :domain => String.t(), :path => String.t(), :expires => number(), :httpOnly => boolean(), :secure => boolean(), :sameSite => String.t(), optional(atom()) => term() } ], :origins => [ %{ :origin => String.t(), :localStorage => [ %{:name => String.t(), :value => String.t(), optional(atom()) => term()} ], optional(atom()) => term() } ], optional(atom()) => term() }
Like storage_state/2, but returns the value and raises on error.
@spec tracing(t()) :: Playwright.Tracing.t()
Tracing recorder for requests made through this API request context.
Since Playwright 1.60.