【问题标题】:JIRA rest api attaching attachment with the issue. how to set MIME typeJIRA rest api 附有问题的附件。如何设置 MIME 类型
【发布时间】:2014-07-24 09:38:10
【问题描述】:

我已在 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 发送请求

【问题讨论】:

    标签: c# asp.net-web-api jira-rest-api


    【解决方案1】:

    我找到了解决方案。我是正确的问题与 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;
    

    这解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 2012-11-02
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多