[파이썬] requests 요청 전/후 처리 커스텀하기

Requests is a popular Python library for making HTTP requests. It provides a convenient and simple way to interact with web services and APIs. However, there may be scenarios where we need to perform custom pre-processing or post-processing tasks before and after making a request using the requests library. In this blog post, we will explore how to customize these pre and post-processing steps.

Pre-processing requests

To customize the pre-processing step before making a request using the requests library, we can take advantage of the Session object. The Session object allows us to persist certain parameters across multiple requests. We can define a custom pre-processing function and attach it to the pre_request attribute of the Session object. This function will be called before each request is sent.

Let’s take a look at an example:

import requests

def preprocess_request(request):
    # Perform necessary pre-processing tasks here
    request.headers['Authorization'] = 'Bearer <your-access-token>'

session = requests.Session()
session.pre_request = preprocess_request

response = session.get('https://api.example.com/data')

In this example, we define a preprocess_request function that updates the request headers with an authorization token. We then attach this function to the pre_request attribute of the Session object. When a request is made with this session object, the preprocess_request function will be called before sending the request.

Post-processing responses

Similarly, we can customize the post-processing step after receiving a response using the requests library. We can define a custom post-processing function and attach it to the post_request attribute of the Session object. This function will be called after each request is made, providing access to the response object.

Here’s an example of post-processing a response:

import requests

def postprocess_response(response):
    # Perform necessary post-processing tasks here
    if response.status_code == 200:
        print("Request was successful")
    else:
        print("Request failed")

session = requests.Session()
session.post_request = postprocess_response

response = session.get('https://api.example.com/data')

In this example, we define a postprocess_response function that checks the status code of the response and prints a message accordingly. We attach this function to the post_request attribute of the Session object. After the request is made with this session object, the postprocess_response function will be called.

By customizing the pre and post-processing steps, we can inject additional functionality and handle specific requirements before and after making requests using the requests library. This flexibility allows us to tailor the requests library to our specific needs and seamlessly integrate it into our Python applications.