The Python requests library is the de facto standard for making HTTP requests. It abstracts the complexities of HTTP communication behind a simple and elegant API, allowing you to focus on interacting with web services and processing data in your application.
In this blog post, we’ll walk through the basics of making GET and POST requests using the Python requests module. By the end, you’ll be equipped to confidently integrate web APIs and external data sources into your Python applications using requests.
What is an API?
API stands for Application Programming Interface. An API is a set of rules that allow different software applications to communicate with each other. Think of it like a bridge that connects two systems and lets them share data or services. For example, when you use a weather app, it likely fetches data from a weather API.
HTTP Request Methods Overview
REST APIs listen for HTTP request methods before taking action. HTTP defines a set of request methods that tell the API what operations to perform for a given resource. It specifies how to interact with the resources located at the provided endpoint. There are several HTTP methods, but five are commonly used with REST APIs:Â
Method | Description |
---|---|
delete(url, args) | The DELETE method deletes the specified resource |
get(url, params, args) | GET method is used to retrieve information from the given server using a given URI. |
head(url, args) | The HEAD method asks for a response identical to that of a GET request, but without the response body. |
patch(url, data, args) | It is used to modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource |
post(url, data, json, args) | POST request method requests that a web server accepts the data enclosed in the body of the request message, most likely for storing it |
put(url, data, args) | The PUT method requests that the enclosed entity be stored under the supplied URI. If the URI refers to an already existing resource, it is modified and if the URI does not point to an existing resource, then the server can create the resource with that URI. |
request(method, url, args) | Sends a request of the specified method to the specified URL |
Installing Python Requests
1 |
pip install requests |
Making a GET Request
In HTTP, the GET
method is used to request a specific resource from a server. This is how you can make an HTTPÂ GET
request with requests.get()
:
1 2 3 4 5 6 7 |
import requests url = "https://jsonplaceholder.typicode.com/posts/1" response = requests.get(url) print("Status Code:", response.status_code) print("Response JSON:", response.json()) |
Explanation:
requests.get(url)
sends a GET request to the URL.response.status_code
tells you if it was successful (200
means OK).response.json()
converts the JSON response to a Python dictionary.
Making a GET Request with parameters
A GET request is used to retrieve data from a server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import requests url = "https://indeed12.p.rapidapi.com/company/Ubisoft/jobs" querystring = {"locality": "us", "start": "1"} headers = { "x-rapidapi-host": "indeed12.p.rapidapi.com", "x-rapidapi-key": "eF0oxv9vKxmshMdX89iWBdY9VAtWp1NBibnjsnhFOcSa3gBgid" } response = requests.get(url, headers=headers, params=querystring) print(response.status_code) print(response.json()) |
Making a POST Request
The HTTPÂ POST
method is used to submit data to a server for further processing. Here is how to make a POST
request with requests.post()
:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import requests url = "https://jsonplaceholder.typicode.com/posts" data = { "title": "Hello World", "body": "This is my first POST request!", "userId": 1 } response = requests.post(url, json=data) print("Status Code:", response.status_code) print("Response JSON:", response.json()) |
Explanation:
-
json=data
automatically converts your Python dictionary into JSON.- The server responds with the newly created data.
DELETE Request
The DELETE
method is used to delete a resource identified by a given URI. This is how to make an HTTPÂ DELETE
request in requests
using the delete()
 method:
1 2 3 4 5 6 7 |
import requests url = "https://jsonplaceholder.typicode.com/posts/1" response = requests.delete(url) print("Status Code:", response.status_code) print("Response Text:", response.text) |
Explanation:
requests.delete(url)
sends a DELETE request.
response.status_code
shows the result (204 or 200).
response.text
shows the server response.
HEAD Request
The HEAD
method is similar to GET
, but it only requests the headers of the response, without the actual body content. So, the response returned by the server for a HEAD
request will be equivalent to that of a GET
request, but with no body data.
1 2 3 4 5 6 7 |
import requests url = "https://jsonplaceholder.typicode.com/posts/1" response = requests.head(url) print("Status Code:", response.status_code) print("Response Headers:", response.headers) |
Explanation:
requests.head(url)
sends a HEAD request.
response.status_code
shows the status.
response.headers
returns response metadata.
PATCH Request
The PATCH
method is used to apply partial modifications to an online resource. Just like for PUT
requests, sending PATCH
requests in the Python requests
library is similar to POST requests. What changes is that the method to employ is requests.patch()
 and the HTTP method string in requests.request()
 is 'PATCH'
.
1 2 3 4 5 6 7 8 |
import requests url = "https://jsonplaceholder.typicode.com/posts/1" data = {"title": "Updated title"} response = requests.patch(url, json=data) print("Status Code:", response.status_code) print("Response JSON:", response.json()) |
Explanation:
requests.patch(url, json=data)
sends partial update data.
response.status_code
shows if it was successful.
response.json()
shows the updated resource.
PUT Request
The PUT
method is used to update or replace a resource on the server. Sending a PUT
request with the Python requests
module is easy and follows a similar pattern as in POST requests. What changes is that the method to use is requests.put()
. Also, the HTTP method string in requests.request()
 will be 'PUT'
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import requests url = "https://jsonplaceholder.typicode.com/posts/1" data = { "id": 1, "title": "New Title", "body": "New content", "userId": 1 } response = requests.put(url, json=data) print("Status Code:", response.status_code) print("Response JSON:", response.json()) |
Explanation:
requests.put(url, json=data)
sends a full update.
response.status_code
shows the result.
response.json()
returns the updated resource.
Tips for Working with APIs using Python Requests
- Always read the API documentation to know what endpoints are available.
- Check required headers (some APIs need authentication tokens).
- Use
try...except
to handle errors gracefully. - Use tools like Postman to test your requests before coding.
Error Handling Example
1 2 3 4 5 6 |
try: response = requests.get("https://jsonplaceholder.typicode.com/posts/99999") response.raise_for_status() print(response.json()) except requests.exceptions.RequestException as e: print("Error:", e) |
Using Python to interact with APIs can open the door to building powerful tools and automating tasks. With just a few lines of code using requests
, you can fetch and send data like a pro! Happy coding!
Leave a Comment