简体   繁体   中英

JIRA rest api attaching attachment with the issue. how to set MIME type

i have successfully attached an image with the issue using JIRA rest API in c# console application. but when i see the issue in jira it gives me error.

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

on searching this issue on the internet. the possible problem seems with the MIME type. when i watch the issue using rest api.(using rest api client postman)

here is my out put

   "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"
        }
    ]

here i see
"mimeType": "image/jpeg" missing in the attachment array.

and i want to know where to add this mime type in my request. my code is as follows

    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;

Please suggest me a solution. i want to use httpclient for sending request

i found the solution. i was correct issue was related to the MIME type. for MultipartFormDataContent i need to give it as follows (Rest of code is written above).

   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;

this fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM