Skip to content
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