How do i upload an attachment to Jira ticket using rest api , using .js ?

Praveen Nulu
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 13, 2018

I need to use Rest API to upload some of our automated runs result reports to Jira tickets using nightwatch java script.

 

Can any one help me fix the 415 response i am getting using the below code :

'Jira: Add Attachment': function (browser) {

//const jira = browser.page.jira();
var request = require('request');

var options = {
method: 'POST',
url: 'https://provinnovate.atlassian.net/rest/api/3/issue/TOQ-633/attachments',

file: {
'file': '/Users/praveennulu/Documents/Github/IntegratedScheduling-master/test_e2e/reports/TestDoc.pdf'
},


headers: {
'Authorization': 'Basic cHJhdmVlbi5udWx1QHByb3ZpZGVuY2Uub3JnOjJ4Nm55WjFpMFdhWFRQRHdKbWJMMDE0Ng==',
'Accept': 'application/json',
'Content-Type': 'application/octet-stream',
'X-Atlassian-Token': 'no-check',
},
};

request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(
'Response: ' + response.statusCode + ' ' + response.statusMessage
);
console.log(body);
});


},

 

 

1 answer

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 14, 2018

Hi Praveen,

The 415 error code is an indication that your REST call is using the incorrect 'Content-type' header for the file you are trying to upload to Jira.  This is explained in more detail in a related Fisheye KB REST API: "Error 415 Unsupported Media Type".  I realize that KB is geared for a different product than your Jira Cloud, but the concept is the same here in terms of file attachments via REST.

Instead of using the header of

'Content-Type': 'application/octet-stream'

I think instead this should be something like

'Content-Type': 'application/pdf'

for this particular pdf file.

I hope this helps.

Andy

Praveen Nulu
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 14, 2018

Even that is also giving me the same 415 response code.

 

The reason for adding the Content-Type: 'application/octet-stream' was due to the below  500 error i was getting when i don't use any  Content-Type .

Response: 500

{"message":"org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/octet-stream","status-code":500,"stack-trace":"java.lang.RuntimeException: org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/octet-stream\n\tat com.atlassian.plugins.rest.common.multipart.fileupload

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 19, 2018

Hi Praveen,

It looks like I was mistaken in my last reply.  Sorry about that.

You don't actually need to define the content-type when uploading a file via the REST API.   I tested this myself using curl and the syntax of

curl --request POST \
  --url 'https://example.atlassian.net/rest/api/3/issue/SCRUM-12/attachments' \
  --header 'Authorization: Basic {myAPItokenhere}' \
  --header 'Accept: application/json' \
  --header 'X-Atlassian-Token: nocheck' \
  -F "file=@/path/to/file/12345.pdf"

I think that you're getting this error because you are trying to define the content-type here when in fact the documentation on using this endpoint does not indicate to use that specific header.  More details in Add attachment POST /rest/api/3/issue/{issueIdOrKey}/attachments.

I believe you should be able to make this work in your python by just removing the 'Content-Type' header entirely.

I hope this helps, please let me know the results.

Andy

Like Rodrigo Angulo likes this
LostInMadness Forever December 2, 2019

if you don't define the content'type u got a 500 status error code........

Like Robert McCool likes this
Nikhil Singh
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 23, 2023

Hi @Praveen Nulu ,

Did you get the answer to this? I am also facing the same issue.

I have added Headers as

1. Content-type: application/json

2. X-Atlassian-Token: nocheck

I have not added 

file: {
'file': '/Users/praveennulu/Documents/Github/IntegratedScheduling-master/test_e2e/reports/TestDoc.pdf'
},

 

but I am adding payload as this

{
    "attachment": [
        {
            "content_type""image/png",
            "file_name""postman.png",
            "size_bytes""1124594",
            "sys_id""afcbf3e1474ab5104b5b9e79e16d4398",
            "Content-Transfer-Encoding""iVBORw0KGgoAAAANSUhEUgAACGgAAAX0CAYAAACfQ617AAAMP2lDQ1BJQ0MgUHJvZmlsZQAASImVVwdYU8kWnluSkEBCCV1K6E0QqQGkhNACSO"
}]
}

Suggest an answer

Log in or Sign up to answer