The Requests library is the go-to package for making HTTP requests in Python. It abstracts the complexities of making requests behind an intuitive API. Though not part of Python’s standard library, it’s worth considering Requests to perform HTTP actions like GET, POST, and more.

By the end of this tutorial, you’ll understand that:

  • Requests is not a built-in Python module—it’s a third-party library that you must install separately.
  • You make a GET request in Python using requests.get() with the desired URL.
  • To add headers to requests, pass a dictionary of headers to the headers parameter in your request.
  • To send POST data, use the data parameter for form-encoded data or the json parameter for JSON data.
  • response.text gives you a string representation of the response content, while response.content provides raw bytes.

This tutorial guides you through customizing requests with headers and data, handling responses, authentication, and optimizing performance using sessions and retries.

If you want to explore the code examples that you’ll see in this tutorial, then you can download them here:

Take the Quiz: Test your knowledge with our interactive “Python's Requests Library” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Python's Requests Library

Test your understanding of the Python Requests library for making HTTP requests and interacting with web services.

Get Started With Python’s Requests Library

Even though the Requests library is a common staple for many Python developers, it’s not included in Python’s standard library. That way, the library can continue to evolve more freely as a self-standing project.

Since Requests is a third-party library, you need to install it before using it in your code. As a good practice, you should install external packages into a virtual environment, but you may choose to install requests into your global environment if you plan to use it across multiple projects.

Whether you’re working in a virtual environment or not, you’ll need to install requests:

Language: Shell
$ python -m pip install requests==2.32.4 

Once pip finishes installing requests, you can use it in your application. Importing requests looks like this:

Language: Python
import requests 

Now that you’re all set up, it’s time to begin your journey with Requests. Your first goal will be to make a GET request.

Make a GET Request

HTTP methods, such as GET and POST, specify the action you want to perform when making an HTTP request. In addition to GET and POST, there are several other common methods that you’ll use later in this tutorial.

One of the most commonly used HTTP methods is GET, which retrieves data from a specified resource. To make a GET request using Requests, you can invoke requests.get().

To try this out, you can make a GET request to GitHub’s REST API by calling get() with the following URL:

Language: Python
>>> import requests >>> requests.get("https://api.github.com") <Response [200]> 

Congratulations! You’ve made your first request. Now you’ll dive a little deeper into the response of that request.

Inspect the Response

A Response is the object that contains the results of your request. Try making that same request again, but this time store the return value in a variable so you can get a closer look at its attributes and behaviors:

Language: Python
>>> import requests >>> response = requests.get("https://api.github.com") 

Read the full article at https://realpython.com/python-requests/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]