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

5 votes
Answer accepted

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 7 hours ago in United States

Atlassian acquires AgileCraft

         Good Day, Bad bad traffic, not sure why!!!! 1/2 hour commute took me 2 hours today 🤯 What helped me is that I kept browsing LinkedIn until...

31 views 1 0
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