Development

Python에서 HTTP 요청은 어떻게하나요?

sonpro 2023. 3. 13. 12:10
반응형

HTTP request

How do I make an HTTP request in Python?

Python is a versatile programming language that can be used for a variety of tasks, including making HTTP requests. In this blog post, we will explore how to make HTTP requests in Python using the requests library.

What is an HTTP request?

HTTP stands for Hypertext Transfer Protocol, which is the protocol used for communication between web servers and clients. An HTTP request is a message sent by a client to a server, requesting a specific action to be performed.

The requests library

The requests library is a popular Python library for making HTTP requests. It provides a simple and intuitive API for making HTTP requests and handling responses.

To use the requests library, you first need to install it. You can do this using pip, the Python package manager, by running the following command:

pip install requests

Once you have installed the requests library, you can import it into your Python code using the following statement:

import requests

Making a GET request

The most common type of HTTP request is a GET request, which is used to retrieve data from a server. To make a GET request using the requests library, you can use the get() method, like this:

import requests

response = requests.get('https://jsonplaceholder.typicode.com/posts')

In this example, we are making a GET request to the JSONPlaceholder API, which returns a list of posts in JSON format. The get() method returns a Response object, which contains the server's response to our request.

Handling the response

Once you have made a request using the requests library, you can access the server's response using the Response object. The Response object has several attributes and methods that allow you to access and manipulate the response data.

For example, you can access the response content as a string using the text attribute:

import requests

response = requests.get('https://jsonplaceholder.typicode.com/posts')
print(response.text)

This will print the response content to the console.

You can also access the response content as bytes using the content attribute:

import requests

response = requests.get('https://jsonplaceholder.typicode.com/posts')
print(response.content)

This will print the response content as bytes to the console.

Making a POST request

In addition to making GET requests, you can also make other types of HTTP requests using the requests library. For example, you can make a POST request to send data to a server.

To make a POST request using the requests library, you can use the post() method, like this:

import requests

data = {'title': 'foo', 'body': 'bar', 'userId': 1}
response = requests.post('https://jsonplaceholder.typicode.com/posts', data=data)

In this example, we are making a POST request to the JSONPlaceholder API, sending a JSON object containing the title, body, and userId of a new post. The post() method returns a Response object, which contains the server's response to our request.

Conclusion

In this blog post, we have explored how to make HTTP requests in Python using the requests library. We have seen how to make GET and POST requests, and how to handle the server's response. The requests library provides a simple and intuitive API for making HTTP requests, making it a great choice for Python developers who need to interact with web APIs.

반응형