Python请求在post d时给出415错误

2024-04-23 12:02:44 发布

您现在位置:Python中文网/ 问答频道 /正文

将数据发布到服务器时出现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)

Tags: 数据代码importauthjsonurldataapplication
1条回答
网友
1楼 · 发布于 2024-04-23 12:02:44

根据MDN Web Docs

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

The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data 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)

相关问题 更多 >