DEV Community

qing
qing

Posted on

REST API Automation With Python: Practical Examples

REST API Automation With Python: Practical Examples

As a developer, you've likely worked with REST APIs at some point in your career. Whether it's fetching data from a third-party service or interacting with your own application's API, REST APIs are a crucial part of modern web development. In this article, we'll explore how to automate REST API interactions using Python.

Introduction to REST API Automation

REST API automation involves using a programming language to send HTTP requests to a REST API, allowing you to automate tasks, fetch data, or interact with the API in other ways. Python is an excellent choice for REST API automation due to its simplicity, flexibility, and extensive libraries.

Required Libraries

To get started with REST API automation in Python, you'll need to install the requests library. You can do this using pip:

pip install requests
Enter fullscreen mode Exit fullscreen mode

The requests library provides a simple and intuitive way to send HTTP requests in Python.

Example 1: Sending a GET Request

Let's start with a simple example: sending a GET request to the JSONPlaceholder API. This API provides a free, publicly accessible API for testing and development purposes.

import requests

# Send a GET request to the JSONPlaceholder API
response = requests.get('https://jsonplaceholder.typicode.com/posts')

# Print the response status code
print(response.status_code)

# Print the response JSON data
print(response.json())
Enter fullscreen mode Exit fullscreen mode

This code sends a GET request to the JSONPlaceholder API and prints the response status code and JSON data.

Example 2: Sending a POST Request

In this example, we'll send a POST request to the JSONPlaceholder API to create a new post.

import requests

# Define the post data
post_data = {
    'title': 'My New Post',
    'body': 'This is the body of my new post',
    'userId': 1
}

# Send a POST request to the JSONPlaceholder API
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=post_data)

# Print the response status code
print(response.status_code)

# Print the response JSON data
print(response.json())
Enter fullscreen mode Exit fullscreen mode

This code sends a POST request to the JSONPlaceholder API with the post data and prints the response status code and JSON data.

Example 3: Sending a PUT Request

In this example, we'll send a PUT request to the JSONPlaceholder API to update an existing post.

import requests

# Define the post data
post_data = {
    'id': 1,
    'title': 'My Updated Post',
    'body': 'This is the updated body of my post',
    'userId': 1
}

# Send a PUT request to the JSONPlaceholder API
response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=post_data)

# Print the response status code
print(response.status_code)

# Print the response JSON data
print(response.json())
Enter fullscreen mode Exit fullscreen mode

This code sends a PUT request to the JSONPlaceholder API with the updated post data and prints the response status code and JSON data.

Example 4: Handling Errors and Exceptions

When working with REST APIs, it's essential to handle errors and exceptions properly. You can use try-except blocks to catch and handle exceptions.

import requests

try:
    # Send a GET request to the JSONPlaceholder API
    response = requests.get('https://jsonplaceholder.typicode.com/posts')

    # Print the response status code
    print(response.status_code)

    # Print the response JSON data
    print(response.json())
except requests.exceptions.RequestException as e:
    # Print the error message
    print(f"Error: {e}")
Enter fullscreen mode Exit fullscreen mode

This code sends a GET request to the JSONPlaceholder API and catches any exceptions that occur during the request. If an exception occurs, it prints the error message.

Conclusion

In this article, we've explored how to automate REST API interactions using Python. We've covered the basics of REST API automation, including sending GET, POST, and PUT requests, as well as handling errors and exceptions. With these examples, you can start automating your own REST API interactions using Python.

Follow me for more Python tips!


💡 Related: **Content Creator Ultimate Bundle (Save 33%)* — $29.99*

Top comments (0)