How to use requests in Python

1 min

The requests library in Python is used to send HTTP requests to a server and receive the response. Here is an example of how to use the requests library to make a GET request to a website:

import requests

response = requests.get("")


In this example, the get() function is used to send a GET request to the website "" and the response is stored in the variable response.

The status_code attribute of the response object can be used to check the status of the request (e.g. 200 for success, 404 for not found, etc.).

The text attribute of the response object contains the HTML content of the website.

You can also pass additional parameters such as headers, cookies, json payload in the request by using the respective functions.

import requests

url = ""
headers = {'Authorization': 'Bearer xxxxxx'}
response = requests.get(url, headers=headers)

It's also possible to send POST requests using the post() function, PUT requests using the put() function, DELETE requests using the delete() function, and so on.

You can also handle different types of response such as json, xml or binary files.

It's worth noting that the requests library is a third-party library and needs to be installed before use by running pip install requests.