Table of Contents

Interface IRequest

Namespace
Microsoft.Playwright
Assembly
Microsoft.Playwright.dll

Whenever the page sends a request for a network resource the following sequence of events are emitted by IPage:

  • Request emitted when the request is issued by the page.
  • Response emitted when/if the response status and headers are received for the request.
  • RequestFinished 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 RequestFailed event is emitted.

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.

public interface IRequest

Remarks

HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will complete with 'requestfinished' event.

Properties

Failure

The method returns null unless this request has failed, as reported by requestfailed event.

**Usage**

Example of logging of all the failed requests:

page.RequestFailed += (_, request) =>
{
    Console.WriteLine(request.Failure);
};
string? Failure { get; }

Property Value

string

Frame

Returns the IFrame that initiated this request.

**Usage**

var frameUrl = request.Frame.Url;

**Details**

Note that in some cases the frame is not available, and this method will throw.

  • When request originates in the Service Worker. You can use request.serviceWorker() to check that.
  • When navigation request is issued before the corresponding frame is created. You can use IsNavigationRequest to check that.

Here is an example that handles all the cases:

IFrame Frame { get; }

Property Value

IFrame

Headers

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 AllHeadersAsync() for complete list of headers that include cookie information.

Dictionary<string, string> Headers { get; }

Property Value

Dictionary<string, string>

IsNavigationRequest

Whether this request is driving frame's navigation.

Some navigation requests are issued before the corresponding frame is created, and therefore do not have Frame available.

bool IsNavigationRequest { get; }

Property Value

bool

Method

Request's method (GET, POST, etc.)

string Method { get; }

Property Value

string

PostData

Request's post body, if any.

string? PostData { get; }

Property Value

string

PostDataBuffer

Request's post body in a binary form, if any.

byte[]? PostDataBuffer { get; }

Property Value

byte[]

RedirectedFrom

Request that was redirected by the server to this one, if any.

When the server responds with a redirect, Playwright creates a new IRequest 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:

var response = await page.GotoAsync("http://www.microsoft.com");
Console.WriteLine(response.Request.RedirectedFrom?.Url); // http://www.microsoft.com

If the website https://google.com has no redirects:

var response = await page.GotoAsync("https://www.google.com");
Console.WriteLine(response.Request.RedirectedFrom?.Url); // null
IRequest? RedirectedFrom { get; }

Property Value

IRequest

RedirectedTo

New request issued by the browser if the server responded with redirect.

**Usage**

This method is the opposite of RedirectedFrom:

Console.WriteLine(request.RedirectedFrom?.RedirectedTo == request); // True
IRequest? RedirectedTo { get; }

Property Value

IRequest

ResourceType

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, other.

string ResourceType { get; }

Property Value

string

Timing

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**

var request = await page.RunAndWaitForRequestFinishedAsync(async () =>
{
    await page.GotoAsync("https://www.microsoft.com");
});
Console.WriteLine(request.Timing.ResponseEnd);
RequestTimingResult Timing { get; }

Property Value

RequestTimingResult

Url

URL of the request.

string Url { get; }

Property Value

string

Methods

AllHeadersAsync()

An object with all the request HTTP headers associated with this request. The header names are lower-cased.

Task<Dictionary<string, string>> AllHeadersAsync()

Returns

Task<Dictionary<string, string>>

HeaderValueAsync(string)

Returns the value of the header matching the name. The name is case insensitive.

Task<string?> HeaderValueAsync(string name)

Parameters

name string

Name of the header.

Returns

Task<string>

HeadersArrayAsync()

An array with all the request HTTP headers associated with this request. Unlike AllHeadersAsync(), header names are NOT lower-cased. Headers with multiple entries, such as Set-Cookie, appear in the array multiple times.

Task<IReadOnlyList<Header>> HeadersArrayAsync()

Returns

Task<IReadOnlyList<Header>>

PostDataJSON()

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.

JsonElement? PostDataJSON()

Returns

JsonElement?

ResponseAsync()

Returns the matching IResponse object, or null if the response was not received due to error.

Task<IResponse?> ResponseAsync()

Returns

Task<IResponse>

SizesAsync()

Returns resource size information for given request.

Task<RequestSizesResult> SizesAsync()

Returns

Task<RequestSizesResult>