Python is a powerful programming language that offers a wide range of libraries and modules that can be used for various tasks. One such library is Requests, which is used for making HTTP requests in Python.
Requests is a Python library that simplifies making HTTP requests in Python. It abstracts the complexities of making requests behind a simple API, allowing you to send HTTP/1.1 requests extremely easily. It is designed to be simple and easy to use, yet powerful and customizable.
In this article, we will discuss how to use the Requests library in Python, with some examples.
Installation
To install the Requests library, you can use pip, the package installer for Python. You can run the following command in your terminal:
pip install requests
Once installed, you can import the library into your Python code using the following statement:
import requests
Making a GET Request
The most common type of HTTP request is a GET request. This is the type of request that is sent when you visit a webpage in your browser. In Requests, you can make a GET request by calling the get() function. Check the example below.
import requests
# send GET request
response = requests.get('https://www.example.com')
print(response.text)
In this example, we are sending a GET request to https://www.example.com. The response from the server is stored in the response variable. We then print the response text, which is the HTML code of the webpage.
Making a POST Request
In addition to GET requests, you can also make POST requests using Requests. POST requests are used to send data to a server, such as submitting a form on a website. Check example below on how you can send POST request.
import requests
data = {'username': 'john', 'password': 'password123'}
response = requests.post('https://www.example.com/login', data=data)
print(response.text)
In this example, we are sending a POST request to https://www.example.com/login. We are also sending data to the server in the form of a dictionary. The response from the server is again stored in the response variable, and we print the response text.
Making a Delete Request
A DELETE request is used to delete a resource on a server. In Requests, you can send a DELETE request using the delete() method. Let's see an example:
import requests
# optional additional json data
data = {'post_id' : 1}
response = requests.delete('https://www.example.com/posts', data=data)
print(response.text)
In this example, we are sending a DELETE request to the API to delete a post with the ID of 1. The we can get thr response text for request being sent.
Making a PATCH Request
A PATCH request is used to update a resource on a server. In Requests, you can send a PATCH request using the patch() method. Let's see an example:
import requests
# set headers to json
headers = {
'Content-Type': 'application/json'
}
# data to send
data = {
'title': 'New Title',
'body': 'New Body',
'post_id' : 1
}
# make patch request
response = requests.patch('https://example.com/posts', headers=headers, json=data)
print(response.text)
In this example, we are sending a PATCH request to the API to update a post. We are sending the updated post data in JSON format in the request body, along with post ID and we are also setting the content type to JSON using the headers parameter. We check the response status code to see if the post was updated successfully.
Handling Response
When you make a request using Requests, you will receive a response object. This object contains a lot of information about the response, including the response code, headers, and content.
Here is an example:
import requests
response = requests.get('https://www.example.com')
print(response.status_code) # response code
if response.status_code == 200:
print("Request success")
else:
print("Request failed")
# get response headers and content
print(response.headers)
print(response.content)
# get json response
proint(response.json())
In this example, we are printing the status code of the response, the headers of the response, and the content of the response.
In this article, we have introduced the Requests library in Python, which is a powerful tool for making HTTP requests. We have shown examples of making GET, POST, DELETE and PATCH requests, as well as handling the response from the server. Requests is a great library to use when working with APIs, web scraping, or just interacting with web servers in general.
Comments (0)