High-level Twisted HTTP Client API for asynchronous HTTP requests in Python
npx @tessl/cli install tessl/pypi-treq@25.5.0A high-level HTTP client library built on top of Twisted's asynchronous networking framework, providing a requests-like API for making HTTP requests in asynchronous Python applications. treq offers comprehensive HTTP method support, automatic content handling, authentication mechanisms, cookie management, and response streaming capabilities.
pip install treqimport treqCommon import pattern for main HTTP functions:
from treq import get, post, put, delete, patch, head, requestContent processing functions:
from treq import collect, content, text_content, json_contentimport treq
from twisted.internet import defer, reactor
@defer.inlineCallbacks
def example():
# Make a simple GET request
response = yield treq.get('https://httpbin.org/get')
body = yield response.text()
print("Response:", body)
# POST with JSON data
response = yield treq.post(
'https://httpbin.org/post',
json={'key': 'value'}
)
data = yield response.json()
print("Posted data:", data['json'])
# POST with form data
response = yield treq.post(
'https://httpbin.org/post',
data={'name': 'example', 'value': '123'}
)
# Request with authentication
response = yield treq.get(
'https://httpbin.org/basic-auth/user/pass',
auth=('user', 'pass')
)
reactor.stop()
# Start the example
example()
reactor.run()treq provides a layered architecture:
treq.*): Simple functions for common HTTP operationsThis design integrates seamlessly with Twisted's reactor-based event loop and supports both callback-based and async/await programming patterns.
Core HTTP request functions supporting all standard methods with comprehensive parameter options for headers, authentication, cookies, timeouts, and data handling.
def get(url, headers=None, **kwargs): ...
def post(url, data=None, **kwargs): ...
def put(url, data=None, **kwargs): ...
def patch(url, data=None, **kwargs): ...
def delete(url, **kwargs): ...
def head(url, **kwargs): ...
def request(method, url, **kwargs): ...Functions for processing HTTP response content with support for streaming, buffered access, automatic encoding detection, and JSON parsing.
def collect(response, collector): ...
def content(response): ...
def text_content(response, encoding="ISO-8859-1"): ...
def json_content(response, **kwargs): ...Advanced HTTP client providing direct access to connection pooling, custom agents, and request lifecycle management for applications requiring fine-grained control.
class HTTPClient:
def __init__(self, agent, cookiejar=None, data_to_body_producer=IBodyProducer): ...
def request(self, method, url, **kwargs): ...
def get(self, url, **kwargs): ...
def post(self, url, data=None, **kwargs): ...HTTP authentication mechanisms including Basic Auth with integration into the Agent architecture for secure credential handling.
def add_basic_auth(agent, username, password): ...
def add_auth(agent, auth_config): ...In-memory testing framework for mocking HTTP requests without network access, enabling fast and reliable unit tests for HTTP-based applications.
class StubTreq:
def __init__(self, resource): ...
# Provides all HTTP methods: get, post, put, delete, patch, head
def flush(self): ...
class RequestTraversalAgent: ...
class StringStubbingResource: ...Cookie handling utilities for creating scoped cookies and managing cookie jars with proper domain and security attribute handling.
def scoped_cookie(origin, name, value): ...
def search(jar, *, domain, name=None): ...Core type definitions used throughout the API:
# URL types
_URLType = Union[str, bytes, EncodedURL, DecodedURL]
# Request data types
_DataType = Union[bytes, io.BytesIO, io.BufferedReader, IBodyProducer, Dict[str, str], List[Tuple[str, str]]]
# Headers types
_HeadersType = Union[Headers, Dict[Union[bytes, str], Union[bytes, str]], Dict[Union[bytes, str], List[Union[bytes, str]]]]
# Cookies types
_CookiesType = Union[CookieJar, Mapping[str, str]]
# Files for upload
_FilesType = Union[Mapping[str, _FileValue], Iterable[Tuple[str, _FileValue]]]
# JSON data
_JSONType = Any