How to upload attachment to an Issue using the REST API in Python??

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

3 answers

1 accepted

4 votes
Accepted answer

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

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

Is it possible to do it in python without using the curl.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 8 hours ago in Canada

Happy New Year - First AUG of the year - great success

Hi Everyone, Hope everyone had a great holiday season and is ready for an exciting 2019.  We had our first AUG last night featuring Tempo Timesheets.  A great presentation and lots of int...

12 views 0 1
View post

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you