简体   繁体   中英

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 output is 415 .

But in case of

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

--> r.status_code output is 200 . Please help me to identify issue.

you forgot to add the headers variable to your post requests. thats why it fails. just add this to your post request and you should get a 201 status code as response:

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

The server application probably doesn't process application/json so when you send it json data it returns 415 error

415 Unsupported Media Type

See here

The GET request seems to work but it may just be sending back a login prompt. POST the data in a format that the server application can process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM