pyreqwest.request

Requests classes and builders.

class ConsumedRequest(Request):

Request that will fully read the response body when sent.

async def send(self) -> pyreqwest.response.Response:

Execute the request returning a Response with fully read response body.

def from_request_and_body(cls, request: Self, body: RequestBody | None) -> Self:

Clone request with a new body instance.

class StreamRequest(Request):

Request whose response body is streamed.

def from_request_and_body(cls, request: Self, body: RequestBody | None) -> Self:

Clone request with a new body instance.

class SyncConsumedRequest(Request):

Synchronous request that will fully read the response body when sent.

def send(self) -> pyreqwest.response.SyncResponse:

Execute the request returning a Response with fully read response body.

def from_request_and_body(cls, request: Self, body: RequestBody | None) -> Self:

Clone request with a new body instance.

class SyncStreamRequest(Request):

Synchronous request whose response body is streamed.

def from_request_and_body(cls, request: Self, body: RequestBody | None) -> Self:

Clone request with a new body instance.

class Request:
def copy(self) -> Self:

Copy the request. Byte-bodies are zero-copied. Stream bodies are re-created via their own copy logic.

def repr_full(self) -> str:

Verbose repr including non-sensitive headers and body summary.

def from_request_and_body(cls, request: Self, body: RequestBody | None) -> Self:

Clone request with a new body instance.

read_buffer_limit
method: str

Get the HTTP method. (e.g. GET, POST).

Get the headers. This is not a copy. Modifying it modifies the request.

Get the url.

body: RequestBody | None

Get the body.

extensions: dict[str, typing.Any]

Arbitrary per-request data storage. Useful for passing through data to middleware and response.

class RequestBuilder(BaseRequestBuilder):

Request builder. Use build() or build_streamed() to create the request to send.

def build(self) -> ConsumedRequest:

Build request that full reads the response body on send().

def build_streamed(self) -> StreamRequest:

Build request whose response body is streamed.

class SyncRequestBuilder(BaseRequestBuilder):

Synchronous request builder. Use build() or build_streamed() to create the request to send.

def build(self) -> SyncConsumedRequest:

Build request that full reads the response body on send().

def build_streamed(self) -> SyncStreamRequest:

Build request whose response body is streamed.

class BaseRequestBuilder:
def error_for_status(self, enable: bool) -> Self:

Enable automatic HTTP error raising (4xx/5xx).

def header(self, name: str, value: str) -> Self:

Append single header value.

def headers(self, headers: Mapping[str, str] | Sequence[tuple[str, str]]) -> Self:

Merge multiple headers (mapping or sequence).

def basic_auth(self, username: str, password: str | None) -> Self:

Add Basic Authorization header.

def bearer_auth(self, token: str) -> Self:

Add Bearer token Authorization header.

def body_bytes(self, body: bytes | bytearray | memoryview) -> Self:

Set body from raw bytes.

def body_text(self, body: str) -> Self:

Set body from text.

def body_json(self, body: Any) -> Self:

Serialize body as JSON. Sets Content-Type header.

def body_stream(self, /, stream):

The type of the None singleton.

def query( self, query: Mapping[str, typing.Any] | Sequence[tuple[str, typing.Any]]) -> Self:

Add/merge query parameters.

def timeout(self, timeout: datetime.timedelta) -> Self:

Set per-request total timeout.

def multipart(self, multipart: pyreqwest.multipart.FormBuilder) -> Self:

Attach multipart form body builder.

def form( self, form: Mapping[str, typing.Any] | Sequence[tuple[str, typing.Any]]) -> Self:

Set application/x-www-form-urlencoded body.

def extensions( self, extensions: Mapping[str, typing.Any] | Sequence[tuple[str, typing.Any]]) -> Self:

Arbitrary per-request data storage. Useful for passing through data to middleware and response.

def with_middleware(self, /, middleware):

The type of the None singleton.

def streamed_read_buffer_limit(self, value: int) -> Self:

Max bytes buffered when reading streamed body.

def default_streamed_read_buffer_limit() -> int:

Default max bytes buffered when reading streamed body.

class RequestBody:

Represents request body content (bytes, text, or async stream). Bodies are single-use.

def from_text(body: str) -> RequestBody:

Create body from text.

def from_bytes(body: bytes | bytearray | memoryview) -> RequestBody:

Create body from raw bytes.

def from_stream( stream: AsyncIterable[bytes] | AsyncIterable[bytearray] | AsyncIterable[memoryview] | Iterable[bytes] | Iterable[bytearray] | Iterable[memoryview]) -> RequestBody:

Create body from async byte stream.

def copy_bytes(self) -> pyreqwest.bytes.Bytes | None:

Return bytes zero-copy. None for stream.

def get_stream( self) -> AsyncIterable[bytes] | AsyncIterable[bytearray] | AsyncIterable[memoryview] | Iterable[bytes] | Iterable[bytearray] | Iterable[memoryview] | None:

Return underlying stream if streaming body else None.