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

Jagruthi k August 10, 2015
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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

7 votes
Answer accepted
rsperafico
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 17, 2015

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

Nathaniel Miller August 16, 2019

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)
1 vote
Rodrigo Silva
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 10, 2015

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

Jagruthi k August 10, 2015

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

0 votes
Jaime S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 11, 2015
TAGS
AUG Leaders

Atlassian Community Events