HTTP POST with JSON Body
Send a POST request with a JSON payload.
Code
Python
import requests
payload = {"name": "Alice", "age": 30}
response = requests.post(
"https://api.example.com/users",
json=payload,
headers={"Authorization": "Bearer token123"}
)
print(response.status_code)
print(response.json())Line-by-line explanation
- 1.Define a dict as the payload.
- 2.Use json=payload to auto-serialize and set Content-Type.
- 3.Add custom headers like Authorization.
- 4.Access status_code and parsed JSON response.
Expected output
201
{'id': 1, 'name': 'Alice'}