I am posting attachment to JIRA rest api through an ajax call but it fails in "the request was rejected because no multipart boundary was found ". I followed the instructions provided on jira doc but still facing this issue. Here is code snipped :
var imageDataUrl = canvas.toDataURL();
$.ajax({
url: "https://example.atlassian.com/rest/api/2/issue/" + issueKeyid + "/attachments",
type: 'POST',
data: {
file: imageDataUrl
},
processData: false,
contentType: 'multipart/form-data',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
xhr.setRequestHeader("X-Atlassian-Token","no-check");
},
success: function(data) {
alert("issue created");
},
error: function(data) {
console.log(data);
}
});
Here imageDataUrl is obtained from html canvas method asDataUrl(canvas). I tried with curl and everything works fine.
What went wrong with code. Is there anything else I have to consider ?
After few hiccups I figured out the way to post attachment to jira. There was something the needed to be done before passing base64 image directly to jira. Here is my final version of code.
var blob = BG.dataURItoBlob(items.screenshotImg);
var fd = new FormData();
fd.append("file", blob);
fd.append('comment', "img");
fd.append('minorEdit', "true");
$.ajax({
url: "https://"+jiraUrl+"/rest/api/2/issue/"+issueKeyid+"/attachments",
type: 'POST',
data: fd,
processData: false,
contentType: false,
headers: {
"X-Atlassian-Token": "nocheck"
},
success: function(data) {
status = "success";
console("success");
},
error:function(data){
status = "failed";
console("Something went wrong !!");
}
});
Here I am converting the base64 image url to blob data first then sending it as a FormData object as a whole to server.
//base64 to blob data
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
},
Sorry about the late reply.
You might need to change the way you assigned your [$.ajax] method. Instead of passing a base 64 url image, You can pass the whole attachment file you wanted to upload that you will also get from your server side (e.g PHP) in which you can post your attachment using [curl].
Assume you've place your [file input] inside a [form] DOM element
<form id="frm-upload" name="frm_upload" enctype="multipart/form-data">
<input type="text" name="input_text" />
<input type="file" name="input_upload_file" />
</form>
<a class="upload"> Upload </a>
Then in your [ajax] callback
$(sElement).click(function(event) {
event.preventDefault();
var oFormData = new FormData($('#frm-upload')[0]); // Assuming this is your form
$.ajax({
url: '/dashboard/upload',
type: 'POST',
data: oFormData,
contentType: false,
processData: false,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa('username' + ":" + 'password'));
xhr.setRequestHeader("X-Atlassian-Token","no-check");
},
success : function(response) {
console.log(response);
}
}, 'json');
});
Then you can get the uploaded file in PHP:
protected function upload()
{
var_dump($_FILES); exit;
}
From which you can have the results:
array(1) {
["input_upload_file"]=>
array(5) {
["name"]=>
string(12) "service6.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(14) "/tmp/php0bYNIY"
["error"]=>
int(0)
["size"]=>
int(1169)
}
}
Which you can use [curl] to do this:
curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -F "file=@service6.png" http://example.atlassian.com/rest/api/2/issue/TEST-123/attachments
As documented in Jira's Post Attachment API:
https://docs.atlassian.com/jira/REST/cloud/?_ga=1.22532412.681630190.1490518356#api/2/issue/{issueIdOrKey}/attachments
Hope this helps for your case.