3

我正在使用 jira api 为问题添加附件

根据文档,我设置了一些东西。

  1. 在请求中提交 X-Atlassian-Token: nocheck 的标头。

  2. 包含附件的 multipart/form-data 参数的名称必须是“file”。

  3. 资源需要一个多部分的帖子。

&当我运行我的代码时,我得到内部服务器错误。

我的代码如下

string postUrl = "http://localhost:8080/rest/api/latest/issue/TES-99/attachments";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); 
client.DefaultRequestHeaders.Add("X-Atlassian-Token", "nocheck");
client.BaseAddress = new System.Uri(postUrl);
byte[] cred = UTF8Encoding.UTF8.GetBytes(credentials);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
var content = new MultipartFormDataContent();
var values = new[]
{
    new KeyValuePair<string, string>("file", "e:\\z.txt")               
};
foreach (var keyValuePair in values)
{
    content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
}
            
var result = client.PostAsync(postUrl, content).Result;

请建议我在哪里犯错

4

2 回答 2

3

我也解决了这个问题。现在我可以使用带有 C# 的 JIRA API 添加附件。

我在这段代码上犯了错误。

var values = new[]
{
    new KeyValuePair<string, string>("file", "e:\\z.txt")               
};

foreach (var keyValuePair in values)
{
    content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
}

这是我的代码。

string postUrl = "http://localhost:8080/rest/api/latest/issue/" + projKey + "/attachments";
      
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

client.DefaultRequestHeaders.Add("X-Atlassian-Token", "nocheck");
client.BaseAddress = new System.Uri(postUrl);
byte[] cred = UTF8Encoding.UTF8.GetBytes(credentials);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));                                                     
MultipartFormDataContent content = new MultipartFormDataContent();

**//The code which solved the problem**  

HttpContent fileContent = new ByteArrayContent(File.ReadAllBytes(filePath));
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(mimeType);
content.Add(fileContent, "file",fileName);
var result = client.PostAsync(postUrl, content).Result;
于 2014-07-24T13:57:42.670 回答
0

这是我的工作代码。

   [HttpPost]
   public async Task<IActionResult> CreateTicketWithAttachent(IFormFile file, [FromQuery] string issuekey)
    {
        try
        {
            string url = $"http://jiraurl/rest/api/2/issue/{issuekey}/attachments";
            var client = new HttpClient();
            var header = new AuthenticationHeaderValue("Basic", "your-auth-key");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Authorization = header;
            client.DefaultRequestHeaders.Add("X-Atlassian-Token", "no-check");

            MultipartFormDataContent multiPartContent = new MultipartFormDataContent("-data-");

            ByteArrayContent byteArrayContent;
            using (var ms = new MemoryStream())
            {
                file.CopyTo(ms);
                var fileBytes = ms.ToArray();
                //string fileString = Convert.ToBase64String(fileBytes);
                byteArrayContent = new ByteArrayContent(fileBytes);
            }

            multiPartContent.Add(byteArrayContent, "file", file.FileName);

            var response = await client.PostAsync(url, multiPartContent);

            var result = response.Content.ReadAsStringAsync().Result;

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
                throw new Exception(result);

            return Ok();
        }
        catch (Exception e)
        {
            return BadRequest(e);               
        }
    }
于 2020-12-21T13:00:02.737 回答