It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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 Nov 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

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 Nov 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

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

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you