How to use python resquests.get to fetch data?

When building a Python application, sending HTTP GET requests often feels like clicking a magic button to fetch data from the web. Yet many developers stumble over crafting URLs, handling query parameters, or parsing the returned JSON cleanly. Have you ever wondered why your API call returns a 400 error when the same URL works in your browser?
The answer often lies in how you pass parameters and headers into requests.get
. By mastering these details, you can avoid silent failures, write cleaner code, and ensure your data pipelines run smoothly without unexpected surprises.
Why Use GET
HTTP defines several methods for interacting with resources. GET is the most common when you want to retrieve information without side effects. Behind the scenes, requests.get
wraps this call in a simple Python function that handles sockets, redirections, and content decoding.
Here’s a quick comparison of GET vs POST:
Method | Purpose |
GET | Read or fetch data |
POST | Create or submit new content |
By choosing GET, you signal to servers and intermediaries that your call is safe, idempotent, and cacheable. This helps performance, respects API design, and aligns with web standards.
Simple GET Example
At its core, making a GET request is straightforward:
import requests
response = requests.get('https://api.example.com/data')
print(response.status_code)
print(response.text)
Key points:
- Always check
response.status_code
before parsing. - Avoid using string concatenation for URLs; it leads to bugs with special characters.
- Use
response.raise_for_status()
to catch HTTP errors early.
Tip: Print
response.url
to verify that query strings or paths are formatted as you expect.
Query Params and Headers
Many APIs require you to pass query parameters or custom headers, such as API keys or tokens. Hard-coding these values into your URL can be error-prone and insecure. Instead, use the built-in params
and headers
arguments:
params = {'page': 2, 'limit': 50}
headers = {'Authorization': 'Bearer YOUR_TOKEN'}
resp = requests.get(
'https://api.example.com/items',
params=params,
headers=headers
)
print(resp.url) # https://api.example.com/items?page=2&limit=50
Practical tips:
- Keep sensitive tokens out of your codebase using environment variables.
- Monitor the final
resp.url
during debugging to catch typos. - Use sessions (shown later) to persist headers across multiple calls.
Parsing JSON Responses
Most modern APIs return JSON. The requests library makes it easy to turn raw text into Python data structures:
data = response.json()
print(type(data)) # usually dict or list
Once you have a dict or list, you can navigate nested fields or iterate over items. If you need advanced control over encoding, see our converting Python objects to JSON strings guide. And when you want to save fetched data to disk, check out our writing JSON to a file tutorial.
Practical tip: Always wrap .json()
in a try/except, as non-JSON responses will raise a ValueError
.
Handling Errors and Status Codes
Checking status codes helps you respond to issues gracefully:
try:
response.raise_for_status()
except requests.HTTPError as err:
print(f"HTTP error occurred: {err}")
else:
process(response.json())
Common status codes:
- 200 OK – request succeeded
- 301/302 – resource moved (redirect)
- 400 Bad Request – invalid input
- 401 Unauthorized – authentication failed
- 403 Forbidden – permission denied
- 404 Not Found – invalid endpoint
- 500+ Server errors
Always log
response.text
orresponse.content
on errors to get API-provided messages.
Sessions and Authentication
For repeated requests or APIs requiring login, use a session:
session = requests.Session()
session.auth = ('user', 'pass')
session.headers.update({'Accept': 'application/json'})
resp = session.get('https://api.example.com/protected')
print(resp.status_code)
Sessions manage cookies, headers, and connection pooling for better performance. You can also attach OAuth tokens or custom retry strategies. Reusing a session avoids TCP overhead and speeds up large data pulls.
Pro tip: Use
requests.adapters.HTTPAdapter
to set up custom retry policies on your session.
Conclusion
Mastering requests.get
unlocks the power to integrate with virtually any web service. From simple endpoint calls to complex authenticated workflows, understanding parameters, error handling, and sessions saves you time and headaches. Next time you fetch data, remember: validate your URLs, protect sensitive headers, and parse JSON safely. With these best practices, your Python apps will be more reliable, maintainable, and performant.
Subscribe to my newsletter
Read articles from Mateen Kiani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
