requests.post 身份验证在 python 中为 jira rest api 提供错误 415

requests.post with Authentication giving error 415 for jira rest api in python

url = "https://example.com/jira/rest/api/2/issue/issue_key/comment"
data = json.dumps({"body": 'some string'})
headers = {'content-type':'application/json'}
r = requests.post(url, data, auth=('username', 'password'))

--> r.status_code 输出为 415.

但如果

r = requests.get(url, data, auth=('username', 'password'))

--> r.status_code 输出为 200。请帮我找出问题。

服务器应用程序可能不处理 application/json 所以当您发送它时 json 数据 returns 415 错误

415 Unsupported Media Type

here

GET 请求似乎有效,但它可能只是发回登录提示。 POST 服务器应用程序可以处理的格式的数据。

您忘记将 headers 变量添加到您的 post 请求中。这就是它失败的原因。只需将此添加到您的 post 请求中,您应该会收到 201 状态代码作为响应:

r = requests.post(url, data, auth=('username', 'password'), headers=headers)