Fetch

A domain for letting clients substitute browser’s network layer with client code.

This CDP domain is experimental.

Types

Generally, you do not need to instantiate CDP types yourself. Instead, the API creates objects for you as return values from commands, and then you can use those objects as arguments to other commands.

class cdp.fetch.RequestId

Unique request identifier.

class cdp.fetch.RequestStage

Stages of the request to handle. Request will intercept before the request is sent. Response will intercept after the response is received (but before response body is received.

REQUEST = 'Request'
RESPONSE = 'Response'
class cdp.fetch.RequestPattern(url_pattern=None, resource_type=None, request_stage=None)
request_stage = None

Stage at wich to begin intercepting requests. Default is Request.

resource_type = None

If set, only requests for matching resource types will be intercepted.

url_pattern = None

Wildcards (‘*’ -> zero or more, ‘?’ -> exactly one) are allowed. Escape character is backslash. Omitting is equivalent to “*”.

class cdp.fetch.HeaderEntry(name, value)

Response HTTP header entry

class cdp.fetch.AuthChallenge(origin, scheme, realm, source=None)

Authorization challenge for HTTP status code 401 or 407.

origin = None

Origin of the challenger.

realm = None

The realm of the challenge. May be empty.

scheme = None

The authentication scheme used, such as basic or digest

source = None

Source of the authentication challenge.

class cdp.fetch.AuthChallengeResponse(response, username=None, password=None)

Response to an AuthChallenge.

password = None

The password to provide, possibly empty. Should only be set if response is ProvideCredentials.

response = None

The decision on what to do in response to the authorization challenge. Default means deferring to the default behavior of the net stack, which will likely either the Cancel authentication or display a popup dialog box.

username = None

The username to provide, possibly empty. Should only be set if response is ProvideCredentials.

Commands

Each command is a generator function. The return type Generator[x, y, z] indicates that the generator yields arguments of type x, it must be resumed with an argument of type y, and it returns type z. In this library, types x and y are the same for all commands, and z is the return type you should pay attention to. For more information, see Getting Started: Commands.

cdp.fetch.continue_request(request_id, url=None, method=None, post_data=None, headers=None)

Continues the request, optionally modifying some of its parameters.

Parameters:
  • request_id (RequestId) – An id the client received in requestPaused event.
  • url (Optional[str]) – (Optional) If set, the request url will be modified in a way that’s not observable by page.
  • method (Optional[str]) – (Optional) If set, the request method is overridden.
  • post_data (Optional[str]) – (Optional) If set, overrides the post data in the request.
  • headers (Optional[List[HeaderEntry]]) – (Optional) If set, overrides the request headrts.
Return type:

Generator[Dict[str, Any], Dict[str, Any], None]

cdp.fetch.continue_with_auth(request_id, auth_challenge_response)

Continues a request supplying authChallengeResponse following authRequired event.

Parameters:
  • request_id (RequestId) – An id the client received in authRequired event.
  • auth_challenge_response (AuthChallengeResponse) – Response to with an authChallenge.
Return type:

Generator[Dict[str, Any], Dict[str, Any], None]

cdp.fetch.disable()

Disables the fetch domain.

Return type:Generator[Dict[str, Any], Dict[str, Any], None]
cdp.fetch.enable(patterns=None, handle_auth_requests=None)

Enables issuing of requestPaused events. A request will be paused until client calls one of failRequest, fulfillRequest or continueRequest/continueWithAuth.

Parameters:
  • patterns (Optional[List[RequestPattern]]) – (Optional) If specified, only requests matching any of these patterns will produce fetchRequested event and will be paused until clients response. If not set, all requests will be affected.
  • handle_auth_requests (Optional[bool]) – (Optional) If true, authRequired events will be issued and requests will be paused expecting a call to continueWithAuth.
Return type:

Generator[Dict[str, Any], Dict[str, Any], None]

cdp.fetch.fail_request(request_id, error_reason)

Causes the request to fail with specified reason.

Parameters:
  • request_id (RequestId) – An id the client received in requestPaused event.
  • error_reason (ErrorReason) – Causes the request to fail with the given reason.
Return type:

Generator[Dict[str, Any], Dict[str, Any], None]

cdp.fetch.fulfill_request(request_id, response_code, response_headers, body=None, response_phrase=None)

Provides response to the request.

Parameters:
  • request_id (RequestId) – An id the client received in requestPaused event.
  • response_code (int) – An HTTP response code.
  • response_headers (List[HeaderEntry]) – Response headers.
  • body (Optional[str]) – (Optional) A response body.
  • response_phrase (Optional[str]) – (Optional) A textual representation of responseCode. If absent, a standard phrase mathcing responseCode is used.
Return type:

Generator[Dict[str, Any], Dict[str, Any], None]

cdp.fetch.get_response_body(request_id)

Causes the body of the response to be received from the server and returned as a single string. May only be issued for a request that is paused in the Response stage and is mutually exclusive with takeResponseBodyForInterceptionAsStream. Calling other methods that affect the request or disabling fetch domain before body is received results in an undefined behavior.

Parameters:request_id (RequestId) – Identifier for the intercepted request to get body for.
Return type:Generator[Dict[str, Any], Dict[str, Any], Tuple[str, bool]]
Returns:A tuple with the following items:
  1. body - Response body.
  2. base64Encoded - True, if content was sent as base64.
cdp.fetch.take_response_body_as_stream(request_id)

Returns a handle to the stream representing the response body. The request must be paused in the HeadersReceived stage. Note that after this command the request can’t be continued as is – client either needs to cancel it or to provide the response body. The stream only supports sequential read, IO.read will fail if the position is specified. This method is mutually exclusive with getResponseBody. Calling other methods that affect the request or disabling fetch domain before body is received results in an undefined behavior.

Parameters:request_id (RequestId) –
Return type:Generator[Dict[str, Any], Dict[str, Any], StreamHandle]
Returns:

Events

Generally, you do not need to instantiate CDP events yourself. Instead, the API creates events for you and then you use the event’s attributes.

class cdp.fetch.RequestPaused(request_id, request, frame_id, resource_type, response_error_reason, response_status_code, response_headers, network_id)

Issued when the domain is enabled and the request URL matches the specified filter. The request is paused until the client responds with one of continueRequest, failRequest or fulfillRequest. The stage of the request can be determined by presence of responseErrorReason and responseStatusCode – the request is at the response stage if either of these fields is present and in the request stage otherwise.

frame_id = None

The id of the frame that initiated the request.

network_id = None

If the intercepted request had a corresponding Network.requestWillBeSent event fired for it, then this networkId will be the same as the requestId present in the requestWillBeSent event.

request = None

The details of the request.

request_id = None

Each request the page makes will have a unique id.

resource_type = None

How the requested resource will be used.

response_error_reason = None

Response error if intercepted at response stage.

response_headers = None

Response headers if intercepted at the response stage.

response_status_code = None

Response code if intercepted at response stage.

class cdp.fetch.AuthRequired(request_id, request, frame_id, resource_type, auth_challenge)

Issued when the domain is enabled with handleAuthRequests set to true. The request is paused until client responds with continueWithAuth.

auth_challenge = None

Details of the Authorization Challenge encountered. If this is set, client should respond with continueRequest that contains AuthChallengeResponse.

frame_id = None

The id of the frame that initiated the request.

request = None

The details of the request.

request_id = None

Each request the page makes will have a unique id.

resource_type = None

How the requested resource will be used.