Requests Library
Make HTTP requests in Python — call REST APIs, send data, handle authentication, and turn the entire internet into your data source.
"The internet is one giant API. With the requests library, every piece of data on the web — weather, prices, news, maps — is just a .get() away."
— ShuraiWhat is requests?
requests is Python’s most popular HTTP library. It lets you send web requests (GET, POST, PUT, DELETE) and handle the responses — without dealing with low-level networking:
pip install requests
GET — Fetch Data from a URL
import requests
response = requests.get("https://httpbin.org/get")
print(response.status_code) # 200 = success
print(response.headers) # dict of response headers
print(response.text) # raw text body
print(response.json()) # parse JSON automatically → dict
200OK — request succeeded, data returned
201Created — POST succeeded, resource created
400Bad Request — something wrong with your request
401Unauthorized — missing or invalid API key
404Not Found — endpoint doesn't exist
429Too Many Requests — rate limited, slow down
500Server Error — problem on the server's side
Query Parameters — Filtering API Results
# Pass params as a dict — requests builds the URL for you
params = {"q": "python", "sort": "stars", "per_page": 3}
resp = requests.get("https://api.github.com/search/repositories",
params=params)
print(resp.url)
# https://api.github.com/search/repositories?q=python&sort=stars&per_page=3
data = resp.json()
for repo in data["items"]:
print(f"{repo['full_name']} — ⭐ {repo['stargazers_count']:,}")
POST — Sending Data
import requests
# POST with JSON body — creating a new resource
payload = {"title": "Buy groceries", "completed": False}
response = requests.post("https://jsonplaceholder.typicode.com/todos",
json=payload)
print(response.status_code) # 201 — created!
print(response.json()) # {'id': 201, 'title': 'Buy groceries', ...}
Headers and Authentication
# API key in a header (most common auth method)
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Accept": "application/json",
}
resp = requests.get("https://api.example.com/data", headers=headers)
# Always check before using the data
resp.raise_for_status() # raises HTTPError if status >= 400
data = resp.json()
Real Example — Weather Checker
import requests
def get_weather(city, lat, lon):
"""Fetch today's temperature for a city."""
url = "https://api.open-meteo.com/v1/forecast"
params = {"latitude": lat, "longitude": lon,
"current_weather": True}
try:
resp = requests.get(url, params=params, timeout=5)
resp.raise_for_status()
weather = resp.json()["current_weather"]
print(f"{city}: {weather['temperature']}°C, wind {weather['windspeed']} km/h")
except requests.exceptions.RequestException as e:
print(f"Error fetching weather for {city}: {e}")
get_weather("Mumbai", 19.08, 72.88)
get_weather("Delhi", 28.61, 77.23)
get_weather("Chennai", 13.08, 80.27)
Without timeout=5, a slow server can hang your program forever. Always wrap requests in try/except for requests.exceptions.RequestException to handle network errors gracefully.
"Three lines of requests code can replace hours of manual data collection. Whenever you find yourself copying data from a website, ask: does this site have an API?"
— Shurai🧠 Quiz — Q1
What does response.json() do?
🧠 Quiz — Q2
You get a 401 status code from an API. What does this mean?
🧠 Quiz — Q3
What is the advantage of passing params={"q": "python"} to requests.get() instead of writing the URL manually?
🧠 Quiz — Q4
Why should you always add timeout=5 to your requests calls?