HTTP library with thread-safe connection pooling, file post support, user friendly interface, and more.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
High-level request interface that provides convenient functions for making HTTP requests without needing to manage connection pools directly. These functions use a module-global PoolManager instance for automatic connection management.
Makes HTTP requests using a shared global PoolManager instance. This is the simplest way to make HTTP requests with urllib3, suitable for scripts and applications that don't need fine-grained connection management.
def request(method: str, url: str, *, body=None, fields=None, headers=None,
preload_content=True, decode_content=True, redirect=True,
retries=None, timeout=3, json=None) -> BaseHTTPResponse:
"""
A convenience, top-level request method using a module-global PoolManager instance.
Parameters:
- method: HTTP request method (GET, POST, PUT, DELETE, etc.)
- url: The URL to perform the request on
- body: Data to send in the request body (str, bytes, iterable, or file-like)
- fields: Data to encode and send in the request body (multipart form data)
- headers: Dictionary of custom headers to send
- preload_content: If True, response body will be preloaded into memory
- decode_content: If True, attempt to decode body based on content-encoding
- redirect: If True, automatically handle redirects (301, 302, 303, 307, 308)
- retries: Configure retry behavior (Retry object, int, bool, or None)
- timeout: Request timeout in seconds (float, int, or Timeout object)
- json: Data to encode and send as JSON with UTF-8 encoding
Returns:
BaseHTTPResponse: Response object with status, headers, and data
"""import urllib3
# Simple GET request
resp = urllib3.request('GET', 'https://httpbin.org/get')
print(f"Status: {resp.status}")
print(f"Data: {resp.data.decode('utf-8')}")import urllib3
# POST request with form fields
resp = urllib3.request('POST', 'https://httpbin.org/post',
fields={'username': 'john', 'password': 'secret'})
print(f"Status: {resp.status}")import urllib3
# POST request with JSON body
resp = urllib3.request('POST', 'https://httpbin.org/post',
json={'message': 'Hello, World!'})
print(f"Status: {resp.status}")import urllib3
# Request with custom headers and timeout
resp = urllib3.request('GET', 'https://httpbin.org/headers',
headers={'User-Agent': 'MyApp/1.0'},
timeout=10)
print(f"Status: {resp.status}")import urllib3
# Request with custom retry behavior
retries = urllib3.Retry(total=5, backoff_factor=0.3)
resp = urllib3.request('GET', 'https://httpbin.org/get',
retries=retries)The body parameter accepts various data types:
read() methodAll requests return a BaseHTTPResponse object with:
status: HTTP status code (int)headers: Response headers (HTTPHeaderDict)data: Response body as bytesread(): Method to read response data in chunksjson(): Method to parse JSON response dataInstall with Tessl CLI
npx tessl i tessl/pypi-urllib3