Playwright.APIRequestContext (Playwright v1.65.0-rc.3)

Copy Markdown View Source

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.

t()

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

clear_cookies_options()

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

delete_options()

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

dispose_options()

@type dispose_options() ::
  [{:reason, String.t()}] | %{optional(:reason) => String.t()}

Options of dispose/2, see its docs.

fetch_options()

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

get_options()

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

head_options()

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

patch_options()

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

post_options()

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

put_options()

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

storage_state_options()

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

t()

@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

add_cookies(api_request_context, cookies)

@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 - Either url or both domain and path are required. Optional.
    • :domain - For the cookie to apply to all subdomains as well, prefix domain with a dot, like this: ".example.com". Either url or both domain and path are required. Optional.
    • :path - Either url or both domain and path are 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.

add_cookies!(api_request_context, cookies)

@spec add_cookies!(t(), [map()]) :: :ok

Like add_cookies/2, but returns the value and raises on error.

clear_cookies(api_request_context, options \\ [])

@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

clear_cookies!(api_request_context, options \\ [])

@spec clear_cookies!(t(), clear_cookies_options()) :: :ok

Like clear_cookies/2, but returns the value and raises on error.

cookies(api_request_context, urls \\ nil)

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

cookies!(api_request_context, urls \\ nil)

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

delete(api_request_context, url, options \\ [])

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

  • :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 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. Use FormData to 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 to false.

  • :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 to 20. Pass 0 to not follow redirects.

  • :max_retries (integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to 0 - no retries.

  • :multipart (term/0 | map/0) - Since Playwright 1.17. 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.

    • :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 an AbortSignal. 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 using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    In Elixir, :infinity is the same as 0: no timeout.

delete!(api_request_context, url, options \\ [])

@spec delete!(t(), String.t(), delete_options()) :: Playwright.APIResponse.t()

Like delete/3, but returns the value and raises on error.

dispose(api_request_context, options \\ [])

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

dispose!(api_request_context, options \\ [])

@spec dispose!(t(), dispose_options()) :: :ok

Like dispose/2, but returns the value and raises on error.

fetch(api_request_context, url_or_request, options \\ [])

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

  • :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 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. Use FormData to 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 to false.

  • :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 to 20. Pass 0 to not follow redirects.

  • :max_retries (integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to 0 - 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 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.

    • :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 an AbortSignal. 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 using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    In Elixir, :infinity is the same as 0: no timeout.

fetch!(api_request_context, url_or_request, options \\ [])

Like fetch/3, but returns the value and raises on error.

get(api_request_context, url, options \\ [])

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

  • :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 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. Use FormData to 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 to false.

  • :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 to 20. Pass 0 to not follow redirects.

  • :max_retries (integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to 0 - no retries.

  • :multipart (term/0 | map/0) - Since Playwright 1.26. 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.

    • :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 an AbortSignal. 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 using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    In Elixir, :infinity is the same as 0: no timeout.

get!(api_request_context, url, options \\ [])

@spec get!(t(), String.t(), get_options()) :: Playwright.APIResponse.t()

Like get/3, but returns the value and raises on error.

head(api_request_context, url, options \\ [])

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

  • :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 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. Use FormData to 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 to false.

  • :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 to 20. Pass 0 to not follow redirects.

  • :max_retries (integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to 0 - no retries.

  • :multipart (term/0 | map/0) - Since Playwright 1.26. 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.

    • :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 an AbortSignal. 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 using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    In Elixir, :infinity is the same as 0: no timeout.

head!(api_request_context, url, options \\ [])

@spec head!(t(), String.t(), head_options()) :: Playwright.APIResponse.t()

Like head/3, but returns the value and raises on error.

patch(api_request_context, url, options \\ [])

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

  • :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 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. Use FormData to 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 to false.

  • :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 to 20. Pass 0 to not follow redirects.

  • :max_retries (integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to 0 - no retries.

  • :multipart (term/0 | map/0) - 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.

    • :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 an AbortSignal. 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 using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    In Elixir, :infinity is the same as 0: no timeout.

patch!(api_request_context, url, options \\ [])

@spec patch!(t(), String.t(), patch_options()) :: Playwright.APIResponse.t()

Like patch/3, but returns the value and raises on error.

post(api_request_context, url, options \\ [])

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

  • :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 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. Use FormData to 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 to false.

  • :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 to 20. Pass 0 to not follow redirects.

  • :max_retries (integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to 0 - no retries.

  • :multipart (term/0 | map/0) - 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.

    • :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 an AbortSignal. 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 using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    In Elixir, :infinity is the same as 0: no timeout.

post!(api_request_context, url, options \\ [])

@spec post!(t(), String.t(), post_options()) :: Playwright.APIResponse.t()

Like post/3, but returns the value and raises on error.

put(api_request_context, url, options \\ [])

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

  • :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 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. Use FormData to 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 to false.

  • :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 to 20. Pass 0 to not follow redirects.

  • :max_retries (integer/0) - Since Playwright 1.46. Maximum number of times network errors should be retried. Currently only ECONNRESET error is retried. Does not retry based on HTTP response codes. The call fails if the limit is exceeded. Defaults to 0 - no retries.

  • :multipart (term/0 | map/0) - 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.

    • :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 an AbortSignal. 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 using Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2; pass timeout: 0 to disable the timeout entirely.

    In Elixir, a Playwright.AbortSignal (Playwright.AbortController.signal/1).

  • :timeout (number/0 | :infinity) - Request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.

    In Elixir, :infinity is the same as 0: no timeout.

put!(api_request_context, url, options \\ [])

@spec put!(t(), String.t(), put_options()) :: Playwright.APIResponse.t()

Like put/3, but returns the value and raises on error.

storage_state(api_request_context, options \\ [])

@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 to true to include IndexedDB in the storage state snapshot.
  • :opfs (boolean/0) - Since Playwright 1.63. Set to true to 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 :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.

storage_state!(api_request_context, options \\ [])

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

tracing(api_request_context)

@spec tracing(t()) :: Playwright.Tracing.t()

Tracing recorder for requests made through this API request context.

Since Playwright 1.60.