Skip to content

Sync Client

These APIs should be used in synchronous apps without asyncio. For asynchronous applications, use asynchronous APIs.

pyqwest.SyncClient

A synchronous HTTP client.

A client is a lightweight wrapper around a SyncTransport, providing convenience methods for common HTTP operations with buffering.

Parameters:

Name Type Description Default
transport SyncTransport | None

The transport to use for requests. If None, the shared default transport will be used.

None

get

Executes a GET HTTP request.

Parameters:

Name Type Description Default
url str

The unencoded request URL.

required
headers Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None

The request headers.

None
timeout float | None

The timeout for the request in seconds.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

Raises:

Type Description
ConnectionError

If the connection fails.

TimeoutError

If the request times out.

ReadError

If an error occurs reading the response.

WriteError

If an error occurs writing the request.

post

Executes a POST HTTP request.

Parameters:

Name Type Description Default
url str

The unencoded request URL.

required
headers Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None

The request headers.

None
content _SyncRequestContent | None

The request content. A Python dictionary will be converted to JSON.

None
timeout float | None

The timeout for the request in seconds.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

Raises:

Type Description
ConnectionError

If the connection fails.

TimeoutError

If the request times out.

ReadError

If an error occurs reading the response.

WriteError

If an error occurs writing the request.

delete

Executes a DELETE HTTP request.

Parameters:

Name Type Description Default
url str

The unencoded request URL.

required
headers Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None

The request headers.

None
timeout float | None

The timeout for the request in seconds.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

Raises:

Type Description
ConnectionError

If the connection fails.

TimeoutError

If the request times out.

ReadError

If an error occurs reading the response.

WriteError

If an error occurs writing the request.

head

Executes a HEAD HTTP request.

Parameters:

Name Type Description Default
url str

The unencoded request URL.

required
headers Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None

The request headers.

None
timeout float | None

The timeout for the request in seconds.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

Raises:

Type Description
ConnectionError

If the connection fails.

TimeoutError

If the request times out.

ReadError

If an error occurs reading the response.

WriteError

If an error occurs writing the request.

options

Executes a OPTIONS HTTP request.

Parameters:

Name Type Description Default
url str

The unencoded request URL.

required
headers Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None

The request headers.

None
timeout float | None

The timeout for the request in seconds.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

Raises:

Type Description
ConnectionError

If the connection fails.

TimeoutError

If the request times out.

ReadError

If an error occurs reading the response.

WriteError

If an error occurs writing the request.

patch

Executes a PATCH HTTP request.

Parameters:

Name Type Description Default
url str

The unencoded request URL.

required
headers Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None

The request headers.

None
content _SyncRequestContent | None

The request content. A Python dictionary will be converted to JSON.

None
timeout float | None

The timeout for the request in seconds.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

Raises:

Type Description
ConnectionError

If the connection fails.

TimeoutError

If the request times out.

ReadError

If an error occurs reading the response.

WriteError

If an error occurs writing the request.

put

Executes a PUT HTTP request.

Parameters:

Name Type Description Default
url str

The unencoded request URL.

required
headers Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None

The request headers.

None
content _SyncRequestContent | None

The request content. A Python dictionary will be converted to JSON.

None
timeout float | None

The timeout for the request in seconds.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

Raises:

Type Description
ConnectionError

If the connection fails.

TimeoutError

If the request times out.

ReadError

If an error occurs reading the response.

WriteError

If an error occurs writing the request.

execute

Executes an HTTP request, returning the full buffered response.

Parameters:

Name Type Description Default
method str

The HTTP method.

required
url str

The unencoded request URL.

required
headers Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None

The request headers.

None
content _SyncRequestContent | None

The request content. A Python dictionary will be converted to JSON.

None
timeout float | None

The timeout for the request in seconds.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

Raises:

Type Description
ConnectionError

If the connection fails.

TimeoutError

If the request times out.

ReadError

If an error occurs reading the response.

WriteError

If an error occurs writing the request.

stream

Executes an HTTP request, allowing the response content to be streamed.

Parameters:

Name Type Description Default
method str

The HTTP method.

required
url str

The unencoded request URL.

required
headers Headers | Mapping[str, str] | Iterable[tuple[str, str]] | None

The request headers.

None
content _SyncRequestContent | None

The request content. A Python dictionary will be converted to JSON.

None
timeout float | None

The timeout for the request in seconds.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

Raises:

Type Description
ConnectionError

If the connection fails.

TimeoutError

If the request times out.

ReadError

If an error occurs reading the response.

WriteError

If an error occurs writing the request.

pyqwest.SyncRequest

An HTTP request.

Parameters:

Name Type Description Default
method str

The HTTP method.

required
url str

The unencoded request URL.

required
headers Headers | None

The request headers.

None
content _SyncRequestContent | None

The request content. A Python dictionary will be converted to JSON.

None
params _QueryParams | None

Query parameters to append to the URL. None values will be treated as key-only.

None

method property

Returns the HTTP method of the request.

url property

Returns the unencoded request URL.

headers property

Returns the request headers.

content property

Returns an iterator over the request content.

pyqwest.SyncTransport

Bases: Protocol

Protocol for synchronous HTTP transport implementations.

The default implementation of SyncTransport is SyncHTTPTransport which issues requests. Custom implementations may be useful to:

  • Mock requests for testing.
  • Add middleware wrapping transports

execute_sync

Executes a request.

pyqwest.SyncHTTPTransport

An HTTP transport implementation using reqwest.

Without any arguments, the transport behaves like the default transport. When creating a transport, take care to set options to meet your needs.

Parameters:

Name Type Description Default
tls_ca_cert bytes | None

The CA certificate to use to verify the server for TLS connections.

None
tls_key bytes | None

The client private key to identify the client for mTLS connections. tls_cert must also be set.

None
tls_cert bytes | None

The client certificate to identify the client for mTLS connections. tls_key must also be set.

None
http_version HTTPVersion | None

The HTTP version to use for requests. If unset, HTTP/1 is used for plaintext and ALPN negotiates the version for TLS connections which typically means HTTP/2 if the server supports it.

None
timeout float | None

Default timeout for requests in seconds. This is the timeout from the start of the request to the end of the response.

None
connect_timeout float | None

Timeout for connection establishment in seconds.

30.0
read_timeout float | None

Timeout for each read operation of a request in seconds.

None
pool_idle_timeout float | None

Timeout for idle connections in the connection pool in seconds.

90.0
pool_max_idle_per_host int | None

Maximum number of idle connections to keep in the pool per host. Defaults to 2.

None
tcp_keepalive_interval float | None

Interval for TCP keepalive probes in seconds.

30.0
enable_gzip bool

Whether to enable gzip decompression for responses.

True
enable_brotli bool

Whether to enable brotli decompression for responses.

True
enable_zstd bool

Whether to enable zstd decompression for responses.

True
use_system_dns bool

Whether to use the system DNS resolver. By default, pyqwest uses an asynchronous DNS resolver implemented in Rust, but it can have different behavior from system DNS in certain environments. Try enabling this option if you have any DNS resolution issues.

False
enable_cookie_store bool

Whether to enable automatic cookie storage and sending. When enabled, the transport will automatically store cookies from responses and send them with subsequent requests.

False

__enter__

Enters the context manager for the transport to automatically close it when leaving.

__exit__

Exits the context manager for the transport, closing it.

execute_sync

Executes the given request, returning the response.

Parameters:

Name Type Description Default
request SyncRequest

The request to execute.

required

close

Closes the transport, releasing any underlying resources.

pyqwest.get_default_sync_transport

Returns the singleton default HTTP transport instance used by synchronous clients that do not specify a transport.ult HTTP transport instance used by clients that do not specify a transport.

The default transport is constructed as follows:

SyncHTTPTransport(
    connect_timeout=30.0,
    pool_idle_timeout=90.0,
    tcp_keepalive_interval=30.0,
    enable_gzip: bool = True,
    enable_brotli: bool = True,
    enable_zstd: bool = True,
)