C
C#14mo ago
acookook

✅ 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
JakenVeina
JakenVeina14mo ago
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?
acookook
acookook14mo ago
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)
acookook
acookook14mo ago
I would like to be able to send any file(s) - of course within the known limits.
Accord
Accord14mo ago
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.
acookook
acookook13mo ago
ok, so the code which does function is the following. The point is simply to get the correct Content (MIME) type.
private RestRequest GenerateRequestForFileUpload(IFormFile file)
{
//Tell Jira what content type should the file be.
var fileExtension = Path.GetExtension(file.FileName).ToLower();
var sourceContentType = _jiraConfiguration.ExtensionToContentType[fileExtension];

// Create a POST request using your favourite library
var request = new RestRequest
{
Authenticator = new HttpBasicAuthenticator("userName","token"),
Method = Method.Post,
AlwaysMultipartFormData = true
};
request.AddHeader("X-Atlassian-Token", "no-check");
request.AddFile("file", GetFileBytes(file), file.FileName, sourceContentType);

return request;
}
private RestRequest GenerateRequestForFileUpload(IFormFile file)
{
//Tell Jira what content type should the file be.
var fileExtension = Path.GetExtension(file.FileName).ToLower();
var sourceContentType = _jiraConfiguration.ExtensionToContentType[fileExtension];

// Create a POST request using your favourite library
var request = new RestRequest
{
Authenticator = new HttpBasicAuthenticator("userName","token"),
Method = Method.Post,
AlwaysMultipartFormData = true
};
request.AddHeader("X-Atlassian-Token", "no-check");
request.AddFile("file", GetFileBytes(file), file.FileName, sourceContentType);

return request;
}
$codegif
Want results from more Discord servers?
Add your server
More Posts
❔ I looking for Game Engine / Framework!Is there any cool Game Engine / Framework✅ Weird values on accessing Database using EF DbContext under throughput load testHello! I'm testing an ASP.NET application that uses an SQL Database. To access this database I defiProperties of IConfigurationSection.Get are null?I have a very simple test case set up using Microsoft.Extensions.Configuration and Microsoft.ExtensiHow do I pass data from my partial view to my main layout in ASP Core?I have this code in my partial view: ``` // Partial View Lesson.html @{ ViewData["LessonTitle"] ✅ [wpf] how to solve autometically closed new window when I create new window..In StickyNotesView.xaml , I create new Window but If I click '+' that new window not only undisplayePlugin attempts to load dependency again, despite it already being loaded?I have a .NET (.NET 7, for posterity) hosting from C++ situation. I followed the .NET hosting tutori❔ Pathing error issue in forms app, only on LinuxHey! So i have this issue currently which i've been somewhat mindboggled over , basically all this f❔ Setting up C# in VSCodeSo I'm trying to learn c# right now, but when I try to run a test program, it just throws an error: ❔ Pan and zoom control zooms into the top left once the content width exceeds the parent width (WPF)I made a control similar to the view box with a horizontal/vertical offset (for panning) and a zoom ❔ how to use List that have struct type ( WPF)public struct Linq { public Window win; public TextBox textBox; } private void newWin