深度阅读

How to set the accept header in an HTTP request in Python?

作者
作者
2023年08月22日
更新时间
9.14 分钟
阅读时间
0
阅读量

To set the Accept header in an HTTP request in Python, you can use the requests library. The Accept header specifies the media types that the client can handle in the response. Here is an example code snippet that demonstrates how to set the Accept header:

import requests

url = 'https://www.example.com/api/data'
headers = {'Accept': 'application/json'}
response = requests.get(url, headers=headers)

# Process the response data

In this example, we create a dictionary object called headers that contains the Accept header with the value application/json, which indicates that the client accepts JSON-encoded data in the response. Then we pass this header as an argument to the get() method of the requests library.

You can modify the value of the Accept header to suit your desired media type. You can also set other headers as required using the headers dictionary object.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。