Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Attachment upload rest API

Peter Griggs November 17, 2020

Hello,

 

I am probably being silly but following the Jira Cloud Rest API documentation (Python) for uploading an attachment to an issue I can't see where to specify the file - the below is the example code. Like I said might just be a long day but I am pulling my hair out!

 

# This code sample uses the 'requests' library: # http://docs.python-requests.org import requests from requests.auth import HTTPBasicAuth import json url = "https://your-domain.atlassian.com/rest/api/3/issue/{issueIdOrKey}/attachments" auth = HTTPBasicAuth("email@example.com", "<api_token>") headers = { "Accept": "application/json" } response = requests.request( "POST", url, headers=headers, auth=auth ) print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

 

Thanks

Peter.

3 answers

0 votes
Michael Ventnor
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 30, 2020

Hi @Peter Griggs ,

The API uses standard multipart form data uploading. If you're using Python, that means using the "files" argument documented here: https://requests.readthedocs.io/en/latest/api/

I haven't used Python in ages, but my reading of the docs is that "files" should be a dictionary. The key must be "file" (the name of the parameter in the Atlassian API docs) and the value must be a tuple: (your filename, your fileobj).

Please also remember to set the "X-Atlassian-Token" header to "no-check" in your "headers" argument.

Peter Griggs November 30, 2020

Thanks will try in the morning. I think the documentation and example needs some work. It really isn't clear.

Peter Griggs December 1, 2020

I'm still no further forward, also "no-check" isn't show in the example in the documentation. I am really chasing my tail.

John Agan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 1, 2020

Hey @Peter Griggs

I just happened upon this thread and haven't tested this answer personally, but I think this example may help:

 

import requests

url = "https://your-domain.atlassian.com/rest/api/3/issue/{issueIdOrKey}/attachments"

# Parse authentication credentials
auth = requests.auth.HTTPBasicAuth('USERNAME','PASSWORD')

# JIRA required header
headers = { 'X-Atlassian-Token': 'no-check' }

# Setup multipart-encoded file
files = [ ('file', (filename, open('/path/to/file.txt','rb'), mime_type)) ]

# Run request
r = requests.post(url, auth=auth, files=files, headers=headers) 
Like Lee Wei Yeong likes this
0 votes
Julian G November 20, 2020

Check this Link out - it might have the information you need to specify the file location within your script

https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-upload-attachment-to-an-Issue-using-the-REST-API-in/qaq-p/464045

Peter Griggs November 30, 2020
0 votes
Peter Griggs November 18, 2020

Bump.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events