gpt4 book ai didi

Python请求在发布数据时给出415错误

转载 作者:行者123 更新时间:2023-12-03 18:30:52 27 4
gpt4 key购买 nike

将数据发布到服务器时出现 415 错误。这是我的代码如何解决这个问题。提前致谢!

import requests
import json
from requests.auth import HTTPBasicAuth
#headers = {'content-type':'application/javascript'}
#headers={'content-type':'application/json', 'Accept':'application/json'}
url = 'http://IPadress/kaaAdmin/rest/api/sendNotification'
data = {"name": "Value"}
r = requests.post(url, auth=HTTPBasicAuth('shany.ka', 'shanky1213'),json=data)
print(r.status_code)

最佳答案

根据 MDN Web Docs ,

The HTTP 415 Unsupported Media Type client error response codeindicates that the server refuses to accept the request because thepayload format is in an unsupported format.

The format problem might be due to the request's indicatedContent-Type or Content-Encoding, or as a result of inspecting thedata directly.


就您而言,我认为您错过了标题。
取消注释
headers={
'Content-type':'application/json',
'Accept':'application/json'
}
包括 headers在您的 POST要求:
r = requests.post(
url,
auth=HTTPBasicAuth('shany.ka', 'shanky1213'),
json=data,
headers=headers
)
应该做的伎俩
import requests
import json
from requests.auth import HTTPBasicAuth


headers = {
'Content-type':'application/json',
'Accept':'application/json'
}
url = 'http://IPadress/kaaAdmin/rest/api/sendNotification'
data = {"name": "Value"}

r = requests.post(
url,
auth=HTTPBasicAuth('shany.ka', 'shanky1213'),
json=data,
headers=headers
)
print(r.status_code)

关于Python请求在发布数据时给出415错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52216808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com