# `Playwright.APIRequest`
[🔗](https://github.com/ftes/playwright_ex/blob/v1.65.0-rc.3/lib/playwright/api/api_request.ex#L7)

Exposes API that can be used for the Web API testing. This class is used for creating
[`APIRequestContext`](`Playwright.APIRequestContext`) instance which in turn can be used for sending web requests. An instance
of this class can be obtained via `Playwright.request/1`. For more information
see [`APIRequestContext`](`Playwright.APIRequestContext`).

*Since Playwright 1.16.*

The examples alias the modules they use (`alias Playwright.{Locator, Page}`).

# `new_context_options`

```elixir
@type new_context_options() ::
  [
    base_url: String.t(),
    client_certificates: [
      %{
        :origin =&gt; String.t(),
        optional(:certPath) =&gt; String.t(),
        optional(:cert) =&gt; binary(),
        optional(:keyPath) =&gt; String.t(),
        optional(:key) =&gt; binary(),
        optional(:pfxPath) =&gt; String.t(),
        optional(:pfx) =&gt; binary(),
        optional(:passphrase) =&gt; String.t()
      }
    ],
    extra_http_headers: %{optional(String.t()) =&gt; String.t()},
    fail_on_status_code: boolean(),
    http_credentials:
      %{
        :username =&gt; String.t(),
        :password =&gt; String.t(),
        optional(:origin) =&gt; String.t(),
        optional(:send) =&gt; String.t()
      }
      | [
          %{
            :username =&gt; String.t(),
            :password =&gt; String.t(),
            optional(:origin) =&gt; String.t(),
            optional(:send) =&gt; String.t()
          }
        ],
    ignore_https_errors: boolean(),
    max_redirects: integer(),
    proxy: %{
      :server =&gt; String.t(),
      optional(:bypass) =&gt; String.t(),
      optional(:username) =&gt; String.t(),
      optional(:password) =&gt; String.t()
    },
    storage_state: String.t() | map(),
    timeout: number() | :infinity,
    user_agent: String.t()
  ]
  | %{
      optional(:base_url) =&gt; String.t(),
      optional(:client_certificates) =&gt; [
        %{
          :origin =&gt; String.t(),
          optional(:certPath) =&gt; String.t(),
          optional(:cert) =&gt; binary(),
          optional(:keyPath) =&gt; String.t(),
          optional(:key) =&gt; binary(),
          optional(:pfxPath) =&gt; String.t(),
          optional(:pfx) =&gt; binary(),
          optional(:passphrase) =&gt; String.t()
        }
      ],
      optional(:extra_http_headers) =&gt; %{optional(String.t()) =&gt; String.t()},
      optional(:fail_on_status_code) =&gt; boolean(),
      optional(:http_credentials) =&gt;
        %{
          :username =&gt; String.t(),
          :password =&gt; String.t(),
          optional(:origin) =&gt; String.t(),
          optional(:send) =&gt; String.t()
        }
        | [
            %{
              :username =&gt; String.t(),
              :password =&gt; String.t(),
              optional(:origin) =&gt; String.t(),
              optional(:send) =&gt; String.t()
            }
          ],
      optional(:ignore_https_errors) =&gt; boolean(),
      optional(:max_redirects) =&gt; integer(),
      optional(:proxy) =&gt; %{
        :server =&gt; String.t(),
        optional(:bypass) =&gt; String.t(),
        optional(:username) =&gt; String.t(),
        optional(:password) =&gt; String.t()
      },
      optional(:storage_state) =&gt; String.t() | map(),
      optional(:timeout) =&gt; number() | :infinity,
      optional(:user_agent) =&gt; String.t()
    }
```

Options of `new_context/2`, see its docs.

# `t`

```elixir
@type t() :: %Playwright.APIRequest{
  conn: term(),
  guid: term(),
  initializer: term(),
  table: term(),
  type: term()
}
```

A `APIRequest` handle. Compare handles with `==`, their fields are internal.

# `new_context`

```elixir
@spec new_context(t(), new_context_options()) ::
  {:ok, Playwright.APIRequestContext.t()} | {:error, Playwright.error()}
```

Creates new instances of [`APIRequestContext`](`Playwright.APIRequestContext`).

*Since Playwright 1.16.*

## Options

* `:base_url` (`t:String.t/0`) - Methods like `Playwright.APIRequestContext.get/3` take the base URL into consideration by using the [`URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) constructor for building the corresponding URL. Examples:

  * baseURL: `http://localhost:3000` and sending request to `/bar.html` results in `http://localhost:3000/bar.html`
  * baseURL: `http://localhost:3000/foo/` and sending request to `./bar.html` results in `http://localhost:3000/foo/bar.html`
  * baseURL: `http://localhost:3000/foo` (without trailing slash) and navigating to `./bar.html` results in `http://localhost:3000/bar.html`
* `:client_certificates` (list of `t:map/0`) - *Since Playwright 1.46.* TLS Client Authentication allows the server to request a client certificate and verify it.

  **Details**

  An array of client certificates to be used. Each certificate object must have either both `certPath` and `keyPath`, a single `pfxPath`, or their corresponding direct value equivalents (`cert` and `key`, or `pfx`). Optionally, `passphrase` property should be provided if the certificate is encrypted. The `origin` property should be provided with an exact match to the request origin that the certificate is valid for.

  Client certificate authentication is only active when at least one client certificate is provided. If you want to reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that does not match any of the domains you plan to visit.

  > #### Note {: .info}
  >
  > When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it work by replacing `localhost` with `local.playwright`.

  * `:origin` - Exact origin that the certificate is valid for. Origin includes `https` protocol, a hostname and optionally a port.
  * `:certPath` - Path to the file with the certificate in PEM format.
  * `:cert` - Direct value of the certificate in PEM format.
  * `:keyPath` - Path to the file with the private key in PEM format.
  * `:key` - Direct value of the private key in PEM format.
  * `:pfxPath` - Path to the PFX or PKCS12 encoded private key and certificate chain.
  * `:pfx` - Direct value of the PFX or PKCS12 encoded private key and certificate chain.
  * `:passphrase` - Passphrase for the private key (PEM or PFX).
* `:extra_http_headers` (`%{optional(String.t()) => String.t()}`) - An object containing additional HTTP headers to be sent with every request. Defaults to none.
* `:fail_on_status_code` (`t:boolean/0`) - *Since Playwright 1.51.* Whether to fail on response codes other than 2xx and 3xx. By default response object is returned
  for all status codes.
* `:http_credentials` (`t:map/0` | list of `t:map/0`) - Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
  If no origin is specified, the username and password are sent to any servers upon unauthorized responses.

  Pass an array to use different credentials for different origins. The first entry that matches the request origin is used, and entries with no origin match any request.

  * `:username`
  * `:password`
  * `:origin` - Restrain sending http credentials on specific origin (scheme://host:port).
  * `:send` - This option only applies to the requests sent from corresponding [`APIRequestContext`](`Playwright.APIRequestContext`) and does not affect requests sent from the browser. `'always'` - `Authorization` header with basic authentication credentials will be sent with the each API request. `'unauthorized` - the credentials are only sent when 401 (Unauthorized) response with `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
* `:ignore_https_errors` (`t:boolean/0`) - Whether to ignore HTTPS errors when sending network requests. Defaults to `false`.
* `:max_redirects` (`t:integer/0`) - *Since Playwright 1.52.* 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. This can be overwritten for each request individually.
* `:proxy` (`t:map/0`) - Network proxy settings.
  * `:server` - Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example
    `http://myproxy.com:3128` or `socks5://myproxy.com:3128`. Short form `myproxy.com:3128` is considered an HTTP
    proxy.
  * `:bypass` - Optional comma-separated domains to bypass proxy, for example `".com, chromium.org,
    .domain.com"`.
  * `:username` - Optional username to use if HTTP proxy requires authentication.
  * `:password` - Optional password to use if HTTP proxy requires authentication.
* `:storage_state` (`t:String.t/0` | `t:map/0`) - Populates context with given storage state. This option can be used to initialize context with logged-in information
  obtained via `Playwright.BrowserContext.storage_state/2` or `Playwright.APIRequestContext.storage_state/2`. Either a path to the
  file with saved storage, or the value returned by one of `Playwright.BrowserContext.storage_state/2` or
  `Playwright.APIRequestContext.storage_state/2` methods.
  * `:cookies`
    * `:name`
    * `:value`
    * `:domain`
    * `:path`
    * `:expires` - Unix time in seconds.
    * `:httpOnly`
    * `:secure`
    * `:sameSite`
  * `:origins`
    * `:origin`
    * `:localStorage`
      * `:name`
      * `:value`
* `:timeout` (`t:number/0` | `:infinity`) - Maximum time in milliseconds to wait for the response. Defaults to
  `30000` (30 seconds). Pass `0` to disable timeout.

  In Elixir, `:infinity` is the same as `0`: no timeout.
* `:user_agent` (`t:String.t/0`) - Specific user agent to use in this context.

# `new_context!`

```elixir
@spec new_context!(t(), new_context_options()) :: Playwright.APIRequestContext.t()
```

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
