✅ Attachment upload to Jira through a custom application using AJAX and ASP .NET Core 6 - Rest Sharp
So I have this ajax snippet with which I send the files to my custom controller, using the IFormFile interface.
function sendFeedback(data) {
$.ajax({
url: '/Jira/SendFeedback',
type: 'POST',
processData: false,
cache: false,
data: populateFormData(data),
contentType: false,
});
}
function populateFormData(data) {
$.each($("input[type='file']")[0].files, function (i, file) {
formData.append('Files', file);
});
return formData;
}
The file(s) I get in the controller and eventually in the the method have those properties in the first image below.
The request is the following:
private RestRequest NewRequest(IFormFile file, JiraCreateIssueResponse issueData)
{
var request = new RestRequest() { Authenticator = new HttpBasicAuthenticator(_jiraConfiguration.ApiUsername, _jiraConfiguration.ApiToken) };
var requestJson = string.Empty;
request.AddHeader("X-Atlassian-Token", "no-check");
request.AlwaysMultipartFormData = true;
//request.AddParameter("file", GetFileBytes(file), file.FileName, "application/octet-stream");
//request.AddFile(file.FileName, GetFileBytes(file), file.FileName);
request.Method = Method.Post;
return request;
}
private byte[] GetFileBytes(IFormFile f)
{
var b = new byte[1];
using (var mS = new MemoryStream())
{
f.CopyTo(mS);
fileBytes = mS.ToArray();
}
return b;
}
private bool SendRequest(RestRequest request, string url, out RestResponse response)
{
var client = new RestClient(url);
response = client.Execute(request);
return response.StatusCode == 200;
}
When I send the request to the correct JIRA API endpoint (v3), I get a HttpSuccess status code, but no attachment is there. What I am doing wrong?6 Replies
You're basically asking about what the proper way is to use the JIRA API
for which the only authoritative answer is "the way the JIRA API documentation says"
a 200 response definitely SUGGESTS that you're not doing anything wrong, so what makes you say that you are?
define "no attachment is there"
are you quite sure this is the correct endpoint to upload an attachment?
are you quite sure you're attaching them to the right, uhh... thing?
Yeah, I sould have defined things more clearly. I was following this doc here https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-issue-issueidorkey-attachments-post and what I would like to arhieve is to have a random file uploaded to the corresponding jira ticket. When I execute this I go to my JIra cloud instance and see no attachment there.
Before I posted this question there I also tried to attach a file before I create a Jira issue to an exististing Jira issue (to not spam issues). I managed to successfully upload the MS Word file, but the file itself was broken (see image below)
Before I posted this question there I also tried to attach a file before I create a Jira issue to an exististing Jira issue (to not spam issues). I managed to successfully upload the MS Word file, but the file itself was broken (see image below)
I would like to be able to send any file(s) - of course within the known limits.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.ok, so the code which does function is the following. The point is simply to get the correct Content (MIME) type.
$codegif