Master HTTP Requests with Python httpx: GET, POST, PUT, Streaming & More
This guide walks you through using the Python httpx library to perform various HTTP methods—including GET, POST, PUT, DELETE, HEAD, and OPTIONS—handle query parameters, decode responses, work with JSON, custom headers, form data, file uploads, streaming, cookies, redirects, and authentication, all with clear code examples.
Import httpx
import httpxFetch a webpage
r = httpx.get("https://httpbin.org/get")
print(r) # <Response [200 OK]>Send an HTTP POST request
r = httpx.post("https://httpbin.org/post", data={"key": "value"})
print(r)PUT, DELETE, HEAD and OPTIONS requests follow the same pattern
r = httpx.put("https://httpbin.org/put", data={"key": "value"})
r = httpx.delete("https://httpbin.org/delete")
r = httpx.head("https://httpbin.org/get")
r = httpx.options("https://httpbin.org/get")Pass parameters in URL
Use the params keyword to send query parameters
params = {"key1": "value2", "key2": ["value2", "value3"]}
r = httpx.get("https://httpbin.org/get", params=params)
print(r.url)Response content
httpx automatically decodes the response body to Unicode text.
r = httpx.get("https://www.example.org/")
print(r.text)
print(r.encoding) # UTF-8
r.encoding = "ISO-8859-1"JSON response
Web APIs usually return JSON.
r = httpx.get("https://api.github.com/events")
print(r.json())Custom headers
url = "http://httpbin.org/headers"
headers = {"user-agent": "my-app/0.0.1"}
r = httpx.get(url, headers=headers)Form data
data = {"key1": "value1", "key1": "value2"}
r = httpx.post("https://httpbin.org/post", data=data)
print(r.text)Form‑encoded data can include multiple values for the same key
data = {"userList": ["shuke", "jack"]}
r = httpx.post("https://httpbin.org/post", data=data)
print(r.text)File upload (multipart)
files = {"upload-file": open("/path/to/file", "rb")}
r = httpx.post("https://httpbin.org/post", files=files)
print(r.text)JSON‑encoded payload
data = {"integer": 123, "boolean": True, "list": ["a", "b", "c"]}
r = httpx.post("https://httpbin.org/post", json=data)
print(r.text)Response status code
r = httpx.get("https://httpbin.org/get")
print(r.status_code) # 200
print(r.status_code == httpx.codes.OK) # TrueResponse headers
print(r.headers)
print(r.headers["Content-Type"])
print(r.headers.get("content-type"))Streaming responses
with httpx.stream("GET", "https://www.example.com") as r:
for chunk in r.iter_bytes():
print(chunk)
for line in r.iter_lines():
print(line)Cookies
r = httpx.get("http://httpbin.org/cookies/set?chocolate=chip", allow_redirects=False)
print(r.cookies["chocolate"])
cookies = {"peanut": "butter"}
r = httpx.get("http://httpbin.org/cookies", cookies=cookies)
print(r.json())Redirects and history
r = httpx.get("http://github.com")
print(r.url)
print(r.status_code)
print(r.history)
# Enable redirects for HEAD request
r = httpx.head("http://github.com/", allow_redirects=True)
print(r.url)Authentication
# Basic or digest authentication
r = httpx.get("https://example.com", auth=("my_user", "password123"))
# Digest authentication
auth = httpx.DigestAuth("my_user", "password123")
r = httpx.get("https://example.com", auth=auth)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
