[파이썬] requests REST API 요청 처리

In today’s tech-driven world, making REST API requests is a common task for many developers. Whether you want to consume data from an external service, interact with a web application, or automate certain processes, having a good understanding of how to handle REST API requests is essential.

Python provides a powerful and user-friendly library called requests that allows you to easily send and receive data via HTTP/HTTPS. In this blog post, we will explore how to handle REST API requests using the requests library in Python.

Installation

Before we get started, let’s make sure you have the requests library installed. Open your command line and run the following command to install it:

pip install requests

Making a GET Request

Let’s begin with a simple example of making a GET request to retrieve data from a REST API endpoint. Here’s the Python code using the requests library:

import requests

url = "https://api.example.com/users"
response = requests.get(url)

if response.status_code == 200:
    data = response.json()
    # Process the retrieved data
else:
    print("Request failed with status code:", response.status_code)

In the code snippet above, we first import the requests module. Then, we define the url variable as the endpoint URL we want to send the GET request to. Using requests.get(url), we send the GET request and store the server’s response in the response variable.

Next, we check the response’s status code. If it’s 200 (indicating a successful request), we can access the response’s JSON data using the response.json() method. From here, we can process the retrieved data as needed. Otherwise, we print an error message with the status code.

Sending POST Data

Sending a POST request with data is another frequently used operation in REST API development. Here’s an example of how to send a POST request with data using the requests library:

import requests

url = "https://api.example.com/users"
data = {
    "name": "John Doe",
    "email": "john.doe@example.com"
}
response = requests.post(url, json=data)

if response.status_code == 201:
    new_user = response.json()
    # Process the newly created user data
else:
    print("Request failed with status code:", response.status_code)

In the above code, we define the url variable as the POST endpoint URL and the data variable as a dictionary containing the data we want to send. Using requests.post(url, json=data), we send the POST request with the JSON encoded data.

After sending the request, we can check the response’s status code. If it’s 201 (indicating a successful resource creation), we can access the response’s JSON data and process it accordingly. Otherwise, we print an error message with the status code.

Conclusion

In this blog post, we have learned how to handle REST API requests using the requests library in Python. We looked at examples of making GET requests and sending POST requests with data.

The requests library provides a convenient and intuitive way to interact with REST APIs. It allows you to easily retrieve data from external services and also send data to create new resources or update existing ones.

With the knowledge gained from this post, you can start building powerful applications that consume and interact with various REST APIs. Happy coding!