0

我已经在 c# 控制台应用程序中使用 JIRA rest API 成功地附加了一个带有问题的图像。但是当我在 jira 中看到这个问题时,它给了我错误。

 Error rendering 'com.atlassian.jira.jira-view-issue-plugin:attachmentmodule'. Please contact your JIRA administrators.

在网上搜索这个问题。可能的问题似乎与 MIME 类型有关。当我使用rest api观看问题时。(使用rest api客户端邮递员)

这是我的输出

   "attachment": [
        {
            "self": "http://localhost:8080/rest/api/2/attachment/10103",
            "id": "10103",
            "filename": "images.jpg",
            "author": {
                "self": "http://localhost:8080/rest/api/2/user?username=wayne",
                "name": "wayne",
                "emailAddress": "brucewayne@waynecorp.com",
                "avatarUrls": {
                    "16x16": "http://www.gravatar.com/avatar/faf8b9c4d44a4d05ca963d512c8d8805?d=mm&s=16",
                    "24x24": "http://www.gravatar.com/avatar/faf8b9c4d44a4d05ca963d512c8d8805?d=mm&s=24",
                    "32x32": "http://www.gravatar.com/avatar/faf8b9c4d44a4d05ca963d512c8d8805?d=mm&s=32",
                    "48x48": "http://www.gravatar.com/avatar/faf8b9c4d44a4d05ca963d512c8d8805?d=mm&s=48"
                },
                "displayName": "The Batman",
                "active": true
            },
            "created": "2014-07-24T12:53:06.080+0530",
            "size": 13303,
            "content": "http://localhost:8080/secure/attachment/10103/images.jpg",
            "thumbnail": "http://localhost:8080/secure/thumbnail/10103/_thumb_10103.png"
        }
    ]

在这里,我看到
附件数组中缺少“mimeType”:“image/jpeg”。

我想知道在我的请求中在哪里添加这种 mime 类型。我的代码如下

    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));


        MultipartFormDataContent content = new MultipartFormDataContent();    

        HttpContent fileContent = new ByteArrayContent(File.ReadAllBytes(filePath));  
            /*
        content.Headers.ContentType=    new MediaTypeHeaderValue("content-type")
        {
            MediaType = System.Net.Mime.MediaTypeNames.Image.Jpeg
        };    */
        content.Add(fileContent, "file", fileName);
        var result = client.PostAsync(postUrl, content).Result;

请建议我一个解决方案。我想使用 httpclient 发送请求

4

1 回答 1

0

我找到了解决方案。我是正确的问题与 MIME 类型有关。对于 MultipartFormDataContent 我需要如下给出(其余代码写在上面)。

   HttpContent fileContent = new ByteArrayContent(File.ReadAllBytes(filePath)
   string mimeType = System.Web.MimeMapping.GetMimeMapping(fileName);

   //specifying MIME Type
   fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(mimeType);

    content.Add(fileContent, "file",fileName);

    var result = client.PostAsync(postUrl, content).Result;

这解决了问题。

于 2014-07-24T13:51:09.153 回答