puppeteer/website/versioned_docs/version-22.8.1/api/puppeteer.httprequest.md
release-please[bot] 042d61030c
chore: release main (#12415)
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
2024-05-13 08:49:32 +00:00

8.2 KiB

sidebar_label
HTTPRequest

HTTPRequest class

Represents an HTTP request sent by a page.

Signature:

export declare abstract class HTTPRequest

Remarks

Whenever the page sends a request, such as for a network resource, the following events are emitted by Puppeteer's page:

  • request: emitted when the request is issued by the page. - requestfinished - emitted when the response body is downloaded and the request is complete.

If request fails at some point, then instead of requestfinished event the requestfailed event is emitted.

All of these events provide an instance of HTTPRequest representing the request that occurred:

page.on('request', request => ...)

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.

The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the HTTPRequest class.

Properties

Property

Modifiers

Type

Description

client

readonly

CDPSession

(Experimental) Warning! Using this client can break Puppeteer. Use with caution.

Methods

Method

Modifiers

Description

abort(errorCode, priority)

Aborts a request.

Remarks:

To use this, request interception should be enabled with Page.setRequestInterception(). If it is not enabled, this method will throw an exception immediately.

abortErrorReason()

The most recent reason for aborting the request

continue(overrides, priority)

Continues request with optional request overrides.

Remarks:

To use this, request interception should be enabled with Page.setRequestInterception().

Exception is immediately thrown if the request interception is not enabled.

continueRequestOverrides()

The ContinueRequestOverrides that will be used if the interception is allowed to continue (ie, abort() and respond() aren't called).

enqueueInterceptAction(pendingHandler)

Adds an async request handler to the processing queue. Deferred handlers are not guaranteed to execute in any particular order, but they are guaranteed to resolve before the request interception is finalized.

failure()

Access information about the request's failure.

Remarks:

fetchPostData()

Fetches the POST data for the request from the browser.

finalizeInterceptions()

Awaits pending interception handlers and then decides how to fulfill the request interception.

frame()

The frame that initiated the request, or null if navigating to error pages.

hasPostData()

True when the request has POST data. Note that HTTPRequest.postData() might still be undefined when this flag is true when the data is too long or not readily available in the decoded form. In that case, use HTTPRequest.fetchPostData().

headers()

An object with HTTP headers associated with the request. All header names are lower-case.

initiator()

The initiator of the request.

interceptResolutionState()

An InterceptResolutionState object describing the current resolution action and priority.

InterceptResolutionState contains: action: InterceptResolutionAction priority?: number

InterceptResolutionAction is one of: abort, respond, continue, disabled, none, or already-handled.

isInterceptResolutionHandled()

Is true if the intercept resolution has already been handled, false otherwise.

isNavigationRequest()

True if the request is the driver of the current frame's navigation.

method()

The method used (GET, POST, etc.)

postData()

The request's post body, if any.

redirectChain()

A redirectChain is a chain of requests initiated to fetch a resource.

Remarks:

redirectChain is shared between all the requests of the same chain.

For example, if the website http://example.com has a single redirect to https://example.com, then the chain will contain one request:

const response = await page.goto('http://example.com');
const chain = response.request().redirectChain();
console.log(chain.length); // 1
console.log(chain[0].url()); // 'http://example.com'

If the website https://google.com has no redirects, then the chain will be empty:

const response = await page.goto('https://google.com');
const chain = response.request().redirectChain();
console.log(chain.length); // 0

resourceType()

Contains the request's resource type as it was perceived by the rendering engine.

respond(response, priority)

Fulfills a request with the given response.

Remarks:

To use this, request interception should be enabled with Page.setRequestInterception().

Exception is immediately thrown if the request interception is not enabled.

response()

A matching HTTPResponse object, or null if the response has not been received yet.

responseForRequest()

The ResponseForRequest that gets used if the interception is allowed to respond (ie, abort() is not called).

url()

The URL of the request