These defaults are the most common surprise when porting code from Requests.
Two defaults differ from Requests and change behaviour:
Timeouts are on. Every request has a 5-second timeout for connecting, reading, writing and acquiring a pooled connection. Slow endpoints raise httpx.ReadTimeout (or ConnectTimeout, WriteTimeout, PoolTimeout), all subclasses of httpx.TimeoutException. Change it per request or per client:
timeout=30.0sets all four to 30 seconds,timeout=httpx.Timeout(10.0, connect=3.0)sets a default and overrides one part,timeout=Nonedisables timeouts entirely (rarely a good idea).
Redirects are not followed. A 301, 302, 307 or 308 comes back as is, with r.is_redirect true, the target in r.headers["location"], and a ready-made r.next_request. Pass follow_redirects=True per request or when creating a client to follow them like Requests does. Followed redirects are listed in r.history.
Not following by default avoids silently sending requests (and credentials) somewhere else, and makes redirects visible. But it means a URL like http://example.com that redirects to HTTPS returns a 301 rather than the page, which looks like a bug until you know.
Other defaults worth knowing: trust_env=True (proxy and SSL environment variables are honoured), certificate verification is on, and HTTP/2 is off until you enable it.
Example
import httpx
r = httpx.get("https://httpbin.org/redirect/1")
print(r.status_code, r.is_redirect) # 302 True
print(r.headers["location"]) # /get
print(r.next_request.url) # https://httpbin.org/get
r = httpx.get("https://httpbin.org/redirect/2", follow_redirects=True)
print(r.status_code, len(r.history)) # 200 2
try:
httpx.get("https://httpbin.org/delay/7") # default timeout is 5 s
except httpx.ReadTimeout:
print("timed out after 5 s") # timed out after 5 s
r = httpx.get("https://httpbin.org/delay/6", timeout=10.0)
print(r.status_code) # 200Remember
- Default timeout: 5 seconds for connect, read, write and pool
- Redirects are not followed unless follow_redirects=True
- Timeout exceptions all subclass httpx.TimeoutException
Watch out for
- Porting Requests code and getting 301/302 responses because follow_redirects was not set.