Simplifying HTTP Requests with the Python Requests Library
In the world of web development, sending and receiving HTTP requests is a common task. However, writing the code to handle these requests can be time-consuming and complex. With Python, there’s a library for just about everything; for HTTP requests, that library is the “requests” library. The Python Requests library was created to make this task much easier. This blog will explore the features and benefits of using the requests library in your Python projects.
What is the Requests Library?
Requests — HTTP for Humans is one of Python’s libraries used for making HTTP requests to a specific URL. It provides a simple interface for sending HTTP/1.1 requests, allowing users to send GET and POST requests, adding headers, form data, and more. The library returns response objects, which can be used to access the response content, headers, and other information about the response.
Pre-requisites
Requests is compatible with both Python 2 and 3, just make sure you have Python installed in your system.
Installation
- Open your terminal or command prompt.
- Run the following command to install the Requests library:
pip install requests
3. Once the installation is finished, you can import the library into your Python project and start making HTTP requests.
import requests
Note: If you’re using a virtual environment, make sure you activate it before running the pip install
command. Also, if you're using Python 2, you might need to use pip2
instead of pip
.
Making a Basic GET Request:
Sending a GET request is as easy as calling the get
method and passing in a URL.
import requests
req = requests.get('https://www.websiteexample.com')
print(req)
The code above returns <Response [200]>
as output, meaning the request is successful and the URL is reachable.
To check the datatype of the req
variable in the code above:
print(type(req))
It returns <class 'requests.models.Response'>
, which means it’s an instance of the Response class. A Response object contains the result of an HTTP request.
In addition, running the code below outputs a status code of 200
, which indicates that the URL is reachable. Then it returns the page’s header data followed by the page’s content.
import requests
req = requests.get('https://www.websiteexample.com')
print('Response Code: ',req.status_code)
print('Response Headers: ',req.headers)
print('Response Content: ',req.text)
Making a POST Request:
Sending a POST request is just as easy. It is used to submit data collected from a web form to a web server. To do this in the Requests library, you need to first create a data dictionary and assign it to the data
argument of the post
method. Let’s take a look:
import requests
url = 'https://www.websiteexample.com'
d = {
'website':'value1',
'courses':['value2','value3']
}
req = requests.post(url, data = d)
print('Response Content:\n',req.text)
After running the code above we’ll get an output similar to the next:
Response Content:
{
"args": {},
"data": "",
"files": {},
"form": {
"courses": [
"value2",
"value3"
],
"website": "value1"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "47",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.27.1",
"X-Amzn-Trace-Id": "Root=1-61e7ec9f-6333082d7f0b73d317acc1f6"
},
"json": null,
"origin": "121.122.65.155",
"url": "https://www.websiteexample.com"
}
Handling Responses:
When you make a request with the Requests library, it returns a response object. This response object has several useful attributes, including status_code
, headers
, and text
. The status_code
attribute is the HTTP status code returned by the server, such as 200 for a successful request or 404 for a resource not found. The headers
attribute is a dictionary of the response headers. The text
attribute is the response body, which can be text, binary data, or anything else.
In conclusion, the requests library makes it easy to send HTTP requests in Python. With just a few lines of code, you can send GET and POST requests, add headers, form data, and more. The library returns response objects, which give you access to the response content, headers, and other information. Whether you’re working on a web app, a script, or any other project that requires HTTP requests, the requests library is a must-have tool in your toolkit.
For more interesting content please Follow me, I’ll appreciate it!
Also, feel free to leave a comment with your feedback, I’m looking forward to hearing your thoughts.