The Python Requests library is one of the most essential modern tools available to Python developers. First released in 2011, it has become the most downloaded Python package today. This handy HTTP library takes a lot of the complexity out of making web requests in Python.
In this comprehensive beginner‘s guide, we‘ll cover the key features of Python Requests to help you make the most of this powerful library. Whether you‘re accessing REST APIs or scraping websites, Requests can handle all your HTTP needs with simple yet robust code. Let‘s dig in!
Why Use the Requests Library?
Before Requests existed, Python only had low-level HTTP libraries like urllib2 or httplib. These were complex to use just to perform simple requests like:
import urllib2
request = urllib2.Request(‘http://example.com‘)
response = urllib2.urlopen(request)
Requests simplifies this process using a more intuitive API:
import requests
response = requests.get(‘http://example.com‘)
Some key advantages of using Requests:
- Simple, yet powerful API for all HTTP methods (GET, POST, PUT, DELETE, etc)
- Easily customizable with headers, authentication, proxies, SSL config
- Automatic parsing of JSON responses
- Connection pooling and session support for better performance
- Wide ecosystem of 3rd party libraries build on top of it
Requests has become the de facto standard for HTTP in Python today, with over 200 million downloads on PyPI as of 2024.
Installing Requests
Requests is available on PyPI and can be installed via pip (the recommended approach):
pip install requests
To upgrade to the latest version:
pip install --upgrade requests
Now let‘s look at how to start using Requests in your code.
Making your First GET Request
Once Requests is installed, you can import it and start making requests:
import requests
response = requests.get(‘https://api.github.com‘)
This will return a Response
object with all details of the HTTP response:
print(response.status_code) # 200
print(response.headers[‘Content-Type‘]) # ‘application/json; charset=utf8‘
print(response.text) # Print raw response content
You can also easily parse JSON response content:
print(response.json())
GET requests allow passing URL parameters via the params
argument:
params = {‘q‘: ‘requests‘, ‘page‘: 2}
response = requests.get(‘https://api.github.com/search/issues‘, params=params)
This builds the final URL as https://api.github.com/search/issues?q=requests&page=2
.
Making POST Requests
A POST request in Requests simply passes data to the data
parameter. This encodes it appropriately:
data = {‘username‘: ‘jdoe‘, ‘password‘: ‘p@ssw0rd‘}
response = requests.post(‘https://example.com/login‘, data=data)
For JSON data, use the json
parameter instead:
data = {‘some‘: ‘payload‘}
response = requests.post(‘https://httpbin.org/post‘, json=data)
You can also pass files using the files
parameter:
files = {‘file‘: open(‘report.xls‘, ‘rb‘)}
requests.post(‘https://example.com/upload‘, files=files)
Handling HTTP Errors
Requests makes it easy to handle HTTP errors like 404 or 500 status codes:
response = requests.get(‘https://api.example.com/nonexistent‘)
if response.status_code == 404:
print(‘API endpoint not found!‘)
elif response.status_code == 500:
print(‘Server error!‘)
Save yourself a headache by always checking status codes before processing response content.
Adding Request Headers
You can add HTTP headers like User-Agent
as a dictionary passed to the headers
parameter:
headers = {‘User-Agent‘: ‘MyBot 1.0‘}
response = requests.get(‘https://example.com‘, headers=headers)
Headers can be used for many purposes like modifying requests or handling authentication.
Using Sessions
For efficiency, Requests supports session objects which persist things like cookies across multiple requests:
session = requests.Session()
session.get(‘https://example.com‘) # Cookies saved for this domain
response = session.get(‘https://example.com/user‘) # Session used here
Sessions are useful to avoid re-sending cookies or redoing authentication each request.
Authentication with Requests
Requests supports multiple authentication schemes that can be used with sessions:
Basic Auth:
from requests.auth import HTTPBasicAuth
requests.get(‘https://api.example.com‘, auth=HTTPBasicAuth(‘user‘, ‘pass‘))
Digest Auth:
from requests.auth import HTTPDigestAuth
url = ‘https://api.example.com‘
requests.get(url, auth=HTTPDigestAuth(‘user‘, ‘pass‘))
OAuth:
import requests
token = ‘xxxxxxx‘
response = requests.get(‘https://api.example.com‘, headers={‘Authorization‘: f‘Bearer {token}‘})
See the Requests Authentication documentation for more details.
Streaming Response Content
For large responses, you may want to stream content incrementally rather than loading the entire body into memory. You can do this with the stream
parameter:
with requests.get(‘https://api.example.com/large_file‘, stream=True) as response:
for chunk in response.iter_content(1024):
print(chunk) # 1024 bytes
This iterates over 1KB chunks of the response to avoid buffering the entire body before processing.
Asynchronous Requests
Requests also provides an asynchronous interface using asyncio:
import asyncio
import requests
async def main():
response = await requests.get(‘https://example.com‘)
print(response.status_code)
asyncio.run(main())
Asyncio usage like this avoids blocking the main thread on IO-bound requests.
Mocking Requests for Testing
The responses library lets you mock out requests made by the Requests library and simulate responses.
This is useful for testing code that relies on external APIs or websites.
import responses
import requests
@responses.activate
def test_example():
responses.add(responses.GET, ‘https://api.example.com‘,
json={‘data‘: ‘value‘})
response = requests.get(‘https://api.example.com‘)
assert response.json() == {‘data‘: ‘value‘}
See the documentation for more details on mock integration.
Finding Help
For more help using Requests, consult these useful resources:
- Requests Official Documentation – Comprehensive docs with examples
- Requests GitHub Repo – View issues and submit questions
- Requests Community Forum – Discuss Questions with other users
- StackOverflow – Requests questions tagged
python-requests
The active Requests open source community is ready to help answer any question!
Conclusion
I hope this guide provided a solid overview of the main features and usage of the versatile Python Requests library. Requests can save you countless hours compared to using clunky low-level HTTP code.
We covered topics like:
- Installing Requests and making basic requests
- Passing parameters, headers, cookies, and data
- Handling HTTP errors and status codes
- Working with response content
- Using sessions for persistence
- Authentication, streaming, asynchronous support
- Mocking and testing Requests
Learning Requests is a must for any Python developer working with HTTP APIs or web services. It empowers you to interact with HTTP servers smoothly and productively.
This just scratches the surface of Requests‘ full potential. Be sure to check out the official documentation and community resources to master Requests and make it your go-to HTTP library. Happy Python requesting!