Python
HTTP Requests
Send HTTP requests using the requests library.
By EZ4Code Team
httprequests
Code
import requests
# GET request
resp = requests.get("https://api.github.com/users/python", timeout=5)
print(resp.status_code)
print(resp.json())
# POST request
resp = requests.post("https://httpbin.org/post", json={"key": "value"})
# With params and headers
resp = requests.get(
"https://api.example.com/data",
params={"page": 1, "size": 20},
headers={"Authorization": "Bearer token"}
)
# Session to keep state
with requests.Session() as s:
s.headers.update({"Authorization": "Bearer token"})
s.get("https://api.example.com/data")Explanation
The requests library simplifies HTTP requests; Session can maintain session state.
More Python Snippets
Sort Dictionary by Value
Sort a Python dictionary by its values in descending order.
List Comprehension
Quickly generate lists using list comprehensions.
Dictionary Merging
Multiple ways to merge dictionaries.
File Read/Write
Various ways to read and write files.
CSV Processing
Read and write CSV files using the csv module.
JSON Processing
JSON serialization and deserialization.