- User Guide#
- Response Content#
- JSON Content#
- Binary Content#
- Using io Wrappers with Response Content#
- Request Data#
- Headers#
- Cookies#
- Query Parameters#
- Form Data#
- JSON#
- Files & Binary Data#
- Certificate Verification#
- Using Timeouts#
- Retrying Requests#
- Errors & Exceptions#
- Logging#
- urllib3#
- Installing#
- Usage#
- Who uses urllib3?#
- License#
- Contributing#
User Guide#
You’ll need a PoolManager instance to make requests. This object handles all of the details of connection pooling and thread safety so that you don’t have to:
import urllib3 # Creating a PoolManager instance for sending requests. http = urllib3.PoolManager() # Sending a GET request and getting back response as HTTPResponse object. resp = http.request("GET", "https://httpbin.org/robots.txt") # Print the returned data. print(resp.data) # b"User-agent: *\nDisallow: /deny\n"
request() returns a HTTPResponse object, the Response Content section explains how to handle various responses. You can use request() to make requests using any HTTP verb:
import urllib3 http = urllib3.PoolManager() resp = http.request( "POST", "https://httpbin.org/post", fields="hello": "world"> # Add custom form fields ) print(resp.data) # b", . >
The Request Data section covers sending other kinds of requests data, including JSON, files, and binary data.
Note For quick scripts and experiments you can also use a top-level urllib3.request() . It uses a module-global PoolManager instance. Because of that, its side effects could be shared across dependencies relying on it. To avoid side effects, create a new PoolManager instance and use it instead. In addition, the method does not accept the low-level **urlopen_kw keyword arguments. System CA certificates are loaded on default.
Response Content#
import urllib3 # Making the request (The request function returns HTTPResponse object) resp = urllib3.request("GET", "https://httpbin.org/ip") print(resp.status) # 200 print(resp.data) # b"\n" print(resp.headers) # HTTPHeaderDict()
JSON Content#
import urllib3 resp = urllib3.request("GET", "https://httpbin.org/ip") print(resp.json()) #
Alternatively, Custom JSON libraries such as orjson can be used to encode data, retrieve data by decoding and deserializing the data attribute of the request:
import orjson import urllib3 encoded_data = orjson.dumps("attribute": "value">) resp = urllib3.request(method="POST", url="http://httpbin.org/post", body=encoded_data) print(orjson.loads(resp.data)["json"]) #
Binary Content#
import urllib3 resp = urllib3.request("GET", "https://httpbin.org/bytes/8") print(resp.data) # b"\xaa\xa5H?\x95\xe9\x9b\x11"
Using io Wrappers with Response Content#
Sometimes you want to use io.TextIOWrapper or similar objects like a CSV reader directly with HTTPResponse data. Making these two interfaces play nice together requires using the auto_close attribute by setting it to False . By default HTTP responses are closed after reading all bytes, this disables that behavior:
import io import urllib3 resp = urllib3.request("GET", "https://example.com", preload_content=False) resp.auto_close = False for line in io.TextIOWrapper(resp): print(line) # # # # . #