Upload a file to a jira issue field (not the attachments field)

HI there,

I’m trying to upload a file to a rich text field on my Jira issue using API but unfortunately I just cant seems to do it.
I’ve succeeded with uploading the file as an attachments into the attachments section but I’m wondering if its even possible to upload it also to a specific field.

In the following code I was trying to upload all the attachments on an issue to a rich text field, but sadly it didn’t work

import base64
from jira import JIRA


jira = JIRA(server='https://xxxxxxxx.atlassian.net/', basic_auth=('xxxxxx.com', 'xxxxxxxxxxxxxxxxxxx'))

# Set the ID of the issue you want to attach the file to
issue_key = "TO-10066"

issue = jira.issue(issue_key)

# Set the ID of the custom field you want to move the attachments to
custom_field_id = "customfield_10680"

attachments = issue.fields.attachment

# Move each attachment to the custom field
for attachment in attachments:
    # Get the file name and contents of the attachment
    file_name = attachment.filename
    file_contents = jira.attachment(attachment.id).get()

    # Attach the file to the issue
    attachment = (file_name, file_contents)

    # Update the custom field with the attachment itself
    issue.update(fields={custom_field_id: attachment})

This is the type of field I want to upload the file into:

Thanks a lot!

Hi @AmitReyhaim
All the images that appear in rich text fields are actually issue attachments. The way it works is, you need to add the files as attachments first, and then add references to them in the text field. If you use the V2 API you do this using wiki markup - with the V3 API it’s more complicated as the payload is some complicated JSON structure.

1 Like

Hi David,
Thank you for you quick response first of all.

I followed your suggestion and tried to use both V2 and V3 API but im still cant get it to upload the file to my custom field.

For both of the examples i was trying to upload to the field an attachment that is already been uploaded before as an attachment.

When trying to use V2 what the output was the this line was just printed as a string into the field:

Here is the V2 API code:

import requests

# Set the URL of the Jira instance and the issue key
url = "https://xxxxxx.atlassian.net/rest/api/2/issue/TO-10066"

# Set the authentication credentials
auth = ("xxxxxxx", "xxxxxxxxx")

# Set the headers
headers = {
    "Content-Type": "application/json"
}

# Set the custom field ID and value
custom_field_id = "customfield_10680"
value = "<ac:image><ac:attachment><ri:attachment ri:filename=\"UploadedByAutomation.png\" /></ac:attachment></ac:image>"

# Set the payload
payload = {
    "fields": {
        custom_field_id: value
    }
}

# Send the request to update the custom field with attachment reference
response = requests.put(url, auth=auth, headers=headers, json=payload)

# Check if the request was successful
if response.status_code == 204:
    print("Custom field updated successfully!")
else:
    print("Failed to update custom field.")

Here is what i got after running it. Its just printing the value to the field as str:

When using V3 API… well it just didnt worked at all :sweat_smile:

Here is the code ive been using:


import requests

# Set the URL of the Jira instance and the issue key
url = "https://xxxxxx.atlassian.net/rest/api/3/issue/TO-10066"

# Set the authentication credentials
auth = ("xxxxxx", "xxxxx")
response = requests.get(url, auth=auth)

# Set the headers
headers = {
    "Content-Type": "application/json"
}

# Set the custom field ID and value
custom_field_id = "customfield_10006"
value = {
    "type": "doc",
    "version": 1,
    "content": [
        {
            "type": "paragraph",
            "content": [
                {
                    "type": "text",
                    "text": "Here is your attachment:"
                },
                {
                    "type": "media",
                    "attrs": {
                        "id": "410806",
                        "type": "attachment",
                        "collection": "410799",
                        "width": 100,
                        "height": 100
                    }
                }
            ]
        }
    ]
}

# Send the request to update the custom field with attachment reference
response = requests.put(url, auth=auth, headers=headers, json={"fields": {custom_field_id: value}})

# Check if the request was successful
if response.status_code == 204:
    print("Custom field updated successfully!")
else:
    print("Failed to update custom field.")

Am i missing something?
Im a little bit new to Jira API and untill now used it for more simple tasks.

The file im trying to upload is a png file if it makes any difference.

Thanks in advance for any help.

Your V2 code is clearly not passing wiki markup. Wiki markup is documented here: Text Formatting Notation Help - Create and track feature requests for Atlassian products.

1 Like

Thank you very much! was able to make it work using this command from V2 documentation

new_value = "!newImage.png!"

Thanks!