Whenever the page sends a request for a network resource the following sequence of events are emitted by Page:
Page.on(:request)emitted when the request is issued by the page.Page.on(:response)emitted when/if the response status and headers are received for the request.Page.on(:request_finished)emitted when the response body is downloaded and the request is complete.
If request fails at some point, then instead of 'requestfinished' event (and possibly instead of 'response' event),
the Page.on(:request_failed) event is emitted.
Note
HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will complete
with 'requestfinished' event.
If request gets a 'redirect' response, the request is successfully finished with the requestfinished event, and a new
request is issued to a redirected url.
Since Playwright 1.8.
The examples alias the modules they use (alias Playwright.{Locator, Page}).
Summary
Functions
An object with all the request HTTP headers associated with this request. The header names are lower-cased.
Like all_headers/1, but returns the value and raises on error.
Returns the Response object if the response has already been received, nil otherwise.
The method returns nil unless this request has failed, as reported by requestfailed event.
Returns the Frame that initiated this request.
Returns the value of the header matching the name. The name is case-insensitive.
Like header_value/2, but returns the value and raises on error.
An object with the request HTTP headers. The header names are lower-cased.
Note that this method does not return security-related headers, including cookie-related ones.
You can use Playwright.Request.all_headers/1 for complete list of headers that include cookie information.
An array with all the request HTTP headers associated with this request. Unlike Playwright.Request.all_headers/1, header names are NOT lower-cased.
Headers with multiple entries, such as Set-Cookie, appear in the array multiple times.
Like headers_array/1, but returns the value and raises on error.
Whether this request is driving frame's navigation.
Request's method (GET, POST, etc.)
Request's post body, if any.
Request's post body in a binary form, if any.
Returns parsed request's body for form-urlencoded and JSON as a fallback if any.
Request that was redirected by the server to this one, if any.
New request issued by the browser if the server responded with redirect.
Contains the request's resource type as it was perceived by the rendering engine. ResourceType will be one of the
following: document, stylesheet, image, media, font, script, texttrack, xhr, fetch, eventsource,
websocket, manifest, beacon, ping, cspreport, other.
Returns the matching Response object, or nil if the response was not received due to error.
Like response/1, but returns the value and raises on error.
The Service Worker that is performing the request.
Returns resource size information for given request.
Like sizes/1, but returns the value and raises on error.
Returns resource timing information for given request. Most of the timing values become available upon the response,
responseEnd becomes available when request finishes. Find more information at
Resource Timing API.
URL of the request.
Types
Functions
@spec all_headers(t()) :: {:ok, %{optional(String.t()) => String.t()}} | {:error, Playwright.error()}
An object with all the request HTTP headers associated with this request. The header names are lower-cased.
Since Playwright 1.15.
Like all_headers/1, but returns the value and raises on error.
@spec existing_response(t()) :: nil | Playwright.Response.t()
Returns the Response object if the response has already been received, nil otherwise.
Unlike Playwright.Request.response/1, this method does not wait for the response to arrive. It returns
immediately with the response object if the response has been received, or nil if the response
has not been received yet.
Since Playwright 1.59.
The method returns nil unless this request has failed, as reported by requestfailed event.
Usage
Example of logging of all the failed requests:
Page.on(page, :request_failed, fn request ->
IO.puts(Request.url(request) <> " " <> Request.failure(request).errorText)
end)Since Playwright 1.8.
@spec frame(t()) :: Playwright.Frame.t()
Returns the Frame that initiated this request.
Usage
frame_url = Request.frame(request) |> Frame.url()Details
Note that in some cases the frame is not available, and this method will fail.
- When request originates in the Service Worker. You can use
Playwright.Request.service_worker/1to check that. - When navigation request is issued before the corresponding frame is created. You can use
Playwright.Request.is_navigation_request/1to check that.
Here is an example that handles all the cases:
cond do
Request.service_worker(request) ->
IO.puts("request #{Request.url(request)} from a service worker")
Request.is_navigation_request(request) ->
IO.puts("request #{Request.url(request)} is a navigation request")
true ->
IO.puts("request #{Request.url(request)} from a frame #{Request.frame(request) |> Frame.url()}")
endSince Playwright 1.8.
@spec header_value(t(), String.t()) :: {:ok, nil | String.t()} | {:error, Playwright.error()}
Returns the value of the header matching the name. The name is case-insensitive.
Since Playwright 1.15.
Arguments
name- Name of the header.
Like header_value/2, but returns the value and raises on error.
An object with the request HTTP headers. The header names are lower-cased.
Note that this method does not return security-related headers, including cookie-related ones.
You can use Playwright.Request.all_headers/1 for complete list of headers that include cookie information.
Since Playwright 1.8.
@spec headers_array(t()) :: {:ok, [%{name: String.t(), value: String.t()}]} | {:error, Playwright.error()}
An array with all the request HTTP headers associated with this request. Unlike Playwright.Request.all_headers/1, header names are NOT lower-cased.
Headers with multiple entries, such as Set-Cookie, appear in the array multiple times.
Since Playwright 1.15.
Like headers_array/1, but returns the value and raises on error.
Request's method (GET, POST, etc.)
Since Playwright 1.8.
Request's post body, if any.
Since Playwright 1.8.
Request's post body in a binary form, if any.
Since Playwright 1.8.
Returns parsed request's body for form-urlencoded and JSON as a fallback if any.
When the response is application/x-www-form-urlencoded then a key/value object of the values will be returned.
Otherwise it will be parsed as JSON.
Since Playwright 1.8.
Request that was redirected by the server to this one, if any.
When the server responds with a redirect, Playwright creates a new Request object. The two requests are connected by
redirectedFrom() and redirectedTo() methods. When multiple server redirects has happened, it is possible to
construct the whole redirect chain by repeatedly calling redirectedFrom().
Usage
For example, if the website http://example.com redirects to https://example.com:
response = Page.goto!(page, "http://example.com")
Response.request(response) |> Request.redirected_from() |> Request.url()
# => "http://example.com/"If the website https://google.com has no redirects:
response = Page.goto!(page, "https://google.com")
Response.request(response) |> Request.redirected_from()
# => nilSince Playwright 1.8.
New request issued by the browser if the server responded with redirect.
Usage
This method is the opposite of Playwright.Request.redirected_from/1:
# prints true
IO.puts(Request.redirected_to(Request.redirected_from(request)) == request)Since Playwright 1.8.
Contains the request's resource type as it was perceived by the rendering engine. ResourceType will be one of the
following: document, stylesheet, image, media, font, script, texttrack, xhr, fetch, eventsource,
websocket, manifest, beacon, ping, cspreport, other.
Since Playwright 1.8.
@spec response(t()) :: {:ok, nil | Playwright.Response.t()} | {:error, Playwright.error()}
Returns the matching Response object, or nil if the response was not received due to error.
Since Playwright 1.8.
@spec response!(t()) :: nil | Playwright.Response.t()
Like response/1, but returns the value and raises on error.
@spec service_worker(t()) :: nil | Playwright.Worker.t()
The Service Worker that is performing the request.
Details
This method is Chromium only. It's safe to call when using other browsers, but it will always be nil.
Requests originated in a Service Worker do not have a Playwright.Request.frame/1 available.
Since Playwright 1.24.
@spec sizes(t()) :: {:ok, %{ requestBodySize: integer(), requestHeadersSize: integer(), responseBodySize: integer(), responseHeadersSize: integer() }} | {:error, Playwright.error()}
Returns resource size information for given request.
Since Playwright 1.15.
@spec sizes!(t()) :: %{ requestBodySize: integer(), requestHeadersSize: integer(), responseBodySize: integer(), responseHeadersSize: integer() }
Like sizes/1, but returns the value and raises on error.
@spec timing(t()) :: %{ startTime: number(), domainLookupStart: number(), domainLookupEnd: number(), connectStart: number(), secureConnectionStart: number(), connectEnd: number(), requestStart: number(), responseStart: number(), responseEnd: number() }
Returns resource timing information for given request. Most of the timing values become available upon the response,
responseEnd becomes available when request finishes. Find more information at
Resource Timing API.
Usage
request_finished_promise = Page.wait_for_event(page, :request_finished)
Page.goto!(page, "http://example.com")
request = Playwright.Promise.await!(request_finished_promise)
Request.timing(request)
# => %{startTime: 1730000000000.0, domainLookupStart: -1, ...}Since Playwright 1.8.
URL of the request.
Since Playwright 1.8.