如果您想发送一个HTTP请求,并且需要将空的请求体发送给服务器,但是服务器返回错误消息,提示“不允许将 {} 作为请求体”,您可以尝试以下解决方法:
import requests
url = "https://example.com/api/endpoint"
headers = {"Content-Type": "application/json"}
response = requests.post(url, headers=headers, json={})
{}
更改为一个空的字符串""
并发送请求。import requests
url = "https://example.com/api/endpoint"
headers = {"Content-Type": "application/json"}
response = requests.post(url, headers=headers, data="")
无论哪种方法,都建议仔细阅读服务器的API文档或与开发人员进行沟通,以确保您正确地发送请求并处理服务器返回的错误消息。