How to use Python Requests
Python Requests is a popular library used for making HTTP requests in Python. It is a simple and easy-to-use library that allows you to send HTTP/1.1 requests using Python.
Installation
You can install Requests using pip
, the package installer for Python.
pip install requests
Sending a GET Request
Sending a GET request using Requests is as easy as calling the get()
method and passing the URL.
import requests
response = requests.get('https://jsonplaceholder.typicode.com/posts')
print(response.status_code)
print(response.content)
In the above example, we are sending a GET request to https://jsonplaceholder.typicode.com/posts
.
- We import the Requests library using the
import
statement. - We then call the
get()
method and pass the URL as an argument. - The
get()
method returns a response object which we store in theresponse
variable. - We can then access the response status code using the
status_code
attribute of the response object. - We can also access the content of the response using the
content
attribute of the response object.
Sending a POST Request
To send a POST request, we can use the post()
method and pass the URL and data.
import requests
data = {
'username': 'john',
'password': 'password123'
}
response = requests.post('https://jsonplaceholder.typicode.com/posts', data=data)
print(response.status_code)
print(response.content)
In the above example, we are sending a POST request to https://jsonplaceholder.typicode.com/posts
with some data.
- We create a dictionary called
data
with some key-value pairs. - We then call the
post()
method and pass the URL and the data dictionary as arguments. - The
post()
method returns a response object which we store in theresponse
variable. - We can then access the response status code using the
status_code
attribute of the response object. - We can also access the content of the response using the
content
attribute of the response object.
Sending a PUT Request
To send a PUT request, we can use the put()
method and pass the URL and data.
import requests
data = {
'title': 'foo',
'body': 'bar',
'userId': 1
}
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', data=data)
print(response.status_code)
print(response.content)
In the above example, we are sending a PUT request to https://jsonplaceholder.typicode.com/posts/1
with some data.
- We create a dictionary called
data
with some key-value pairs. - We then call the
put()
method and pass the URL and the data dictionary as arguments. - The
put()
method returns a response object which we store in theresponse
variable. - We can then access the response status code using the
status_code
attribute of the response object. - We can also access the content of the response using the
content
attribute of the response object.
Sending a DELETE Request
To send a DELETE request, we can use the delete()
method and pass the URL.
import requests
response = requests.delete('https://jsonplaceholder.typicode.com/posts/1')
print(response.status_code)
print(response.content)
In the above example, we are sending a DELETE request to https://jsonplaceholder.typicode.com/posts/1
.
- We call the
delete()
method and pass the URL as an argument. - The
delete()
method returns a response object which we store in theresponse
variable. - We can then access the response status code using the
status_code
attribute of the response object. - We can also access the content of the response using the
content
attribute of the response object.
Error Handling
Requests also provides error handling for HTTP errors. If a request returns an error status code (400 or above), Requests will raise an exception.
import requests
try:
response = requests.get('https://jsonplaceholder.typicode.com/invalid')
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
In the above example, we are sending a GET request to an invalid URL. Requests will raise a HTTPError
exception, which we catch and print the error message.
Conclusion
Python Requests is a simple and easy-to-use library for making HTTP requests in Python. It provides methods for sending GET, POST, PUT and DELETE requests, as well as error handling for HTTP errors. With Python Requests, you can easily interact with APIs and web services.