Python Requests Example Tutorial For Beginners With Code Snippets

Logan Jul 13, 2026

Python's `requests` library is a robust and elegant solution for making HTTP requests in your Python programs, enabling everything from simple API calls to complex web scraping. It abstracts a lot of the complexity and marker-up involved with making HTTP requests in Python, making it a popular choice for developers.

Python Requests Library: GET, POST & DELETE Examples | API Cheat Sheet
Python Requests Library: GET, POST & DELETE Examples | API Cheat Sheet

In this guide, we'll explore the basics of Python's `requests` library with practical examples. Whether you're a seasoned developer or just starting out, understanding how to use `requests` can greatly enhance your Python toolkit. Let's dive in!

Python Request Module
Python Request Module

Installation and Importing `requests`

Before you can start making HTTP requests, you need to install the `requests` library. You can do this using pip:

Python Requests: PUT, DELETE & Error Handling Guide | API Cheat Sheet Part 2
Python Requests: PUT, DELETE & Error Handling Guide | API Cheat Sheet Part 2

pip install requests

Once installed, importing the library is as simple as:

import requests

Sending a GET Request

🌍✨ PYTHON FOR WORKING WITH APIs: INTEGRATE WITH THE WEB ✨🌍
🌍✨ PYTHON FOR WORKING WITH APIs: INTEGRATE WITH THE WEB ✨🌍

A GET request retrieves data from a specified resource. Here's how you can send a GET request using `requests`:

response = requests.get('https://api.github.com')

This will make a GET request to the GitHub API. You can access the JSON response using `response.json()`:

data = response.json() print(data)

Parametrizing GET Requests

Python Remove Substring with Examples
Python Remove Substring with Examples

Sometimes, you want to send additional parameters with your GET request. You can do this using the `params` parameter:

response = requests.get('https://api.github.com/users', params={'search': 'jacob'} )

This will send a GET request to the GitHub users API, searching for users with the word 'jacob' in their name.

Sending Other Kinds of Requests

API Error Handling in Python: Complete Guide with Code Examples
API Error Handling in Python: Complete Guide with Code Examples

`requests` supports various other HTTP methods, like POST, PUT, DELETE, etc. Here's how you can send a POST request:

Sending a POST Request

a screen shot of a web page with the text send a post request with data
a screen shot of a web page with the text send a post request with data
Python Requests Tutorial
Python Requests Tutorial
Master Python List Methods 🐍 | Beginner-Friendly Cheat Sheet
Master Python List Methods 🐍 | Beginner-Friendly Cheat Sheet
python
python
Reverse a List in Python (Easy Method for Beginners)
Reverse a List in Python (Easy Method for Beginners)
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Python Web Dev Tip: How a Request Flows Through Your App
Python Web Dev Tip: How a Request Flows Through Your App
a yellow and white poster with the names of different types of items on it's side
a yellow and white poster with the names of different types of items on it's side
How to Print Exception in Python with Examples
How to Print Exception in Python with Examples
JSON & Python Data Types: Complete Mapping Guide | API Development
JSON & Python Data Types: Complete Mapping Guide | API Development
Python Lists in Python Programming
Python Lists in Python Programming
Build Your First Python API in 6 Steps | Flask Tutorial for Beginners
Build Your First Python API in 6 Steps | Flask Tutorial for Beginners
Python array sorting guide #python #programming #tutorial
Python array sorting guide #python #programming #tutorial
Python Notes for Beginners (Easy + Quick Guide)
Python Notes for Beginners (Easy + Quick Guide)
Python Next Function (With Examples)
Python Next Function (With Examples)
a computer screen showing the different types of commands and symbols for each type of device
a computer screen showing the different types of commands and symbols for each type of device
Python Errors and Exception Handling | Codevisionz
Python Errors and Exception Handling | Codevisionz
Python Variables Tutorial for Beginners 🚀
Python Variables Tutorial for Beginners 🚀
Python Errors — Types, Causes, Examples & Explanation
Python Errors — Types, Causes, Examples & Explanation
Python List Methods
Python List Methods

POST requests are used to send data to a server. Let's create a new user on the JSONPlaceholder API:

response = requests.post('https://jsonplaceholder.typicode.com/users', json={ 'name': 'John Doe', 'username': 'johndoe', 'email': 'johndoe@example.com' } )

In this example, we're sending a JSON object as data. The `json` parameter automatically sets the request header to `application/json`.

Handling Response Data

After making a request, the server responds with status codes that indicate the outcome of the request. You can access these status codes using the `status_code` attribute:

if response.status_code == 200: print('Request was successful!') else: print(f'Request failed with status code {response.status_code}')

The server also sends back response data. You can access this using the `text`, `json`, or `content` attributes, depending on the type of data you're expecting:

print(response.json())

This will print the JSON response, if the server returned JSON data.

Error Handling

When making HTTP requests, it's important to handle potential errors gracefully. Here's how you can use `try-except` to catch and handle `requests` exceptions:

Using try-except

Here's a simple example of how you can handle exceptions using `try-except`:

try: response = requests.get('https://api.github.com/non-existent-user') response.raise_for_status() except requests.HTTPError as http_err: print(f'HTTP error occurred: {http_err}') except Exception as err: print(f'Other error occurred: {err}')

The `raise_for_status()` method raises an `HTTPError` if one occurred, allowing you to handle it in a specific `except` clause.

Whenever you're working with `requests`, remember that the internet is an unreliable place. Always handle potential errors and edge cases to make your code robust and reliable. Now you're ready to start making HTTP requests like a pro!