[파이썬] requests POST 요청 기본사항

When working with APIs or web applications, sending POST requests is a common task. In Python, one popular library for making HTTP requests is Requests. In this blog post, we will discuss the basic concepts of making POST requests with the Requests library.

Installing Requests

Before we start, make sure you have the Requests library installed. If not, you can install it using pip:

pip install requests

Making a POST Request

To make a POST request using the Requests library, we need to use the requests.post() method. This method takes two required arguments:

  1. URL: The URL where we want to send the POST request.
  2. Data: The data that we want to send in the request body. This data can be in various formats such as JSON, XML, or form-encoded data.

Sending JSON Data in POST Request

Let’s start with an example of sending JSON data in a POST request. Assume we want to send a JSON payload to the URL https://api.example.com/create.

import requests
import json

# Define the URL and data
url = 'https://api.example.com/create'
data = {'name': 'John Doe', 'age': 25, 'email': 'john@example.com'}

# Convert data to JSON format
json_data = json.dumps(data)

# Make the POST request
response = requests.post(url, data=json_data)

# Print the response
print(response.json())

In the above code, we first import the requests and json modules. Then, we define the URL and the data in a dictionary format. We convert the data to JSON format using json.dumps(). Finally, we make the POST request using requests.post() method.

Sending Form-Encoded Data in POST Request

Now, let’s see an example of sending form-encoded data in a POST request. Assume we want to send a POST request to the URL https://api.example.com/login with username and password.

import requests

# Define the URL and data
url = 'https://api.example.com/login'
data = {'username': 'johndoe', 'password': 'secretpassword'}

# Make the POST request
response = requests.post(url, data=data)

# Print the response
print(response.text)

In the above code, we define the URL and the data in a dictionary format. We make the POST request using requests.post() method without converting the data to JSON format. We print the response using response.text.

Handling Other Request Headers and Parameters

Apart from the URL and data, you can also pass additional headers, authentication credentials, cookies, and query parameters in the POST request. You can do this by providing additional arguments to the requests.post() method.

For example, to send a POST request with headers and authentication credentials:

import requests

# Define the URL and data
url = 'https://api.example.com/create'
data = {'name': 'John Doe', 'age': 25, 'email': 'john@example.com'}

# Define headers and authentication credentials
headers = {'Content-Type': 'application/json'}
auth = ('username', 'password')

# Make the POST request
response = requests.post(url, json=data, headers=headers, auth=auth)

# Print the response
print(response.json())

In the above code, we define the URL, data, headers, and authentication credentials. We pass these arguments in the requests.post() method using the json, headers, and auth parameters.

Conclusion

In this blog post, we discussed the basic concepts of making POST requests in Python using the Requests library. We learned how to send JSON and form-encoded data in a POST request and how to handle additional request headers and parameters. The Requests library provides an easy-to-use interface for making HTTP requests, making it a popular choice for developers.