I want to upload an attachment to an Issue in Jira using the Rest API. How can I do the following in Python.
curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt" http://myhost/rest/api/2/issue/TEST-123/attachments
Community moderators have prevented the ability to post new answers.
Hello
Thank you for your question.
Please, run the following in Python:
# =============== # greeting.txt # =============== hello, world # =============== # upload.attachment.py # =============== import requests import json # [JIRA-BASE-URL], i.e.: http://localhost:8080 # [ISSUE-ID], i.e.: ATT-1 url = '[JIRA-BASE-URL]/rest/api/2/issue/[ISSUE-ID]/attachments' headers = {"X-Atlassian-Token": "nocheck"} # please uncomment to attach external file files = {'file': open('greeting.txt', 'rb')} # upload file to issue # [USERNAME], i.e.: admin # [PASSWORD], i.e.: admin r = requests.post(url, auth=('[USERNAME]', '[PASSWORD]'), files=files, headers=headers) print(r.status_code) print(r.text)
If you find this answer useful, I would kindly ask you to accept it so the same will be visible to others who might be facing the same issue you have inquired.
Thank you for your understanding.
—
Kind regards,
Rafael P. Sperafico
Atlassian Support
Great post, made the difference for my efforts. The one thing I would mention is that at least for me (2019, Python2, CentOS 7) I had to do the following for the authentication to work:
credentials = requests.auth.HTTPBasicAuth('[USERNAME]','[PASSWORD]')
r = requests.post(url, auth=credentials, files=files, headers=headers)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jagruthi,
I believe the answer you are looking for might be related on this article:
https://docs.atlassian.com/jira/REST/6.2/#d2e2310
Cheers,
Rodrigo Silva
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here's an equivalent of curl in python (I didn't test it, by the way):
These are also useful:
Kind regards,
Jaime Kirch da Silveira
Atlassian Cloud Support
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.