JIRA REST API如何使用c#添加附件

时间:2014-07-23 15:29:09

标签: jira asp.net-web-api jira-rest-api jira-rest-java-api

我正在使用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;

请建议我犯错的地方

2 个答案:

答案 0 :(得分:1)

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

我错误地使用了这段代码。

     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;

答案 1 :(得分: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);               
        }
    }