π Quick Info
- Topic: Making HTTP requests in Python
- Target Audience: Beginners who know functions and JSON
- Goal: Understand how to fetch data from the internet with Python
1. Introduction
"Every app I used was pulling data from somewhere. Weather apps, social media, online stores β they all talk to servers. Then I learned about APIs and HTTP requests, and I could finally build programs that talk to the internet."
2. What Is an API?
An API (Application Programming Interface) is a way for two programs to talk to each other.
When you open a weather app, it doesn't know the weather itself. It asks a server: "What's the weather in Karachi?" The server responds with data. That's an API.
Think of it as a waiter in a restaurant β you don't walk into the kitchen. You tell the waiter what you want. The waiter brings it back.
3. The Problem (Without APIs)
Imagine trying to get the weather manually:
- Open a browser
- Go to a weather site
- Read the temperature
- Copy it down
- Repeat every hour
Not scalable. Not automatable. You need a way for code to ask for data directly.
4. The Solution (Using requests)
import requests
response = requests.get("<https://api.github.com/users/torvalds>")
data = response.json()
print(data["name"])
print(data["public_repos"])
Output:
Linus Torvalds
100+
You just fetched live data from Githubβs API in 4 lines.
5. How It Works (Line By Line)
-
Line 1:
import requestsβ Loads the requests library (you may need to install it first withpip install requests). -
Line 3:
requests.get(url)β Sends an HTTP GET request to the URL. -
Line 4:
.json()β Converts the response to a Python dictionary. -
Line 6:
data["name"]β Accesses a specific field.
6. HTTP Status Codes
The server always tells you what happened:
| Code | Meaning |
|---|---|
| 200 | OK- everything worked |
| 404 | Not Found- The URL doesn't exist |
| 403 | Forbidden- you donβt have access |
| 500 | Server error- the server broke |
Always check the status code:
if response.status_code == 200:
data = response.json()
else:
print(f"Error: {response.status_code}")
7. Real Example (Weather API)
import requests
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": 24.86,
"longitude": 67.01,
"current_weather": True
}
response = requests.get(url, params=params)
data = response.json()
print(f"Temperature: {data['current_weather']['temperature']}Β°C")
Output:
Temperature: 32.4Β°C
8. Common Mistakes Beginners Make
Mistake 1: Not installing requests
pip install requests
Mistake 2: Forgetting .json()
response is not the data β it's the response object. You need .json() to get the actual content.
Mistake 3: Not handling errors
Networks fail. URLs break. Always check status_code.
Mistake 4: Hardcoding API keys
If an API needs a key, store it in an environment variable, not in your code.
9. What I learned
- APIs let your code talk to the internet
- requests.get() sends a request
- .json() turns the response into a Python dictionary
- Status codes tell you what happened
- Always handle errors β networks are not reliable
10. Conclusion
"APIs felt like magic when I first used one. Now they're just... tools. Small pieces of code that fetch data and give me back a dictionary. Once you build your first API call, you'll want to build twenty more.β
Top comments (0)