0
votes

Im trying to attach file with atlassian jira api using python requests module. Attachment is successful but name of the attachment becomes file and thumbnail of the attachment is improper.Below is the screenshot of attachment from jira after successful ticket creation.

enter image description here

Python code given below. "Content-type" : "multipart/form-data" is directly passed from Front-End javascript code. Please note that atlassian documentation mentions that the name of the multipart/form-data parameter that contains the attachments must be file.

file = request.files['file']
files = {'file': file.read()}
response = requests.post(JIRA_URL + '/' + path,
                         headers={'X-Atlassian-Token': 'no-check'},
                         auth=(JIRA_USER, JIRA_PASS),
                         files=files,
                         proxies={'https': PROXY})
1

1 Answers

0
votes

I use jira-python and then code such as this

        with open(localname, 'rb') as fd:
            jira.add_attachment(target_issue, attachment=fd, filename=att_filename)

where localname is the local filename, and att_filename is the actual filename you want to see appear in the issue.

Inside the jira-python code there is

    url = self._get_url('issue/' + str(issue) + '/attachments')

    fname = filename
    if not fname:
        fname = os.path.basename(attachment.name)

    if 'MultipartEncoder' not in globals():
        method = 'old'
        r = self._session.post(
            url,
            files={
                'file': (fname, attachment, 'application/octet-stream')},
            headers=CaseInsensitiveDict({'content-type': None, 'X-Atlassian-Token': 'nocheck'}))
    else:
        method = 'MultipartEncoder'

        def file_stream():
            return MultipartEncoder(
                fields={
                    'file': (fname, attachment, 'application/octet-stream')})
        m = file_stream()
        r = self._session.post(
            url, data=m, headers=CaseInsensitiveDict({'content-type': m.content_type, 'X-Atlassian-Token': 'nocheck'}), retry_data=file_stream)

which is a bit fiddly. I really recommend using jira-python rather than requests directly