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.

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!

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

pip install requests
Once installed, importing the library is as simple as:
import requests
Sending a GET Request

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

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

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




















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!