# Upload file using REST API

**URL:** <https://forum.kintone.dev/t/upload-file-using-rest-api/174>\
**Category:** General\
**Created:** [November 7, 2018, 7:36am UTC](https://forum.kintone.dev/t/upload-file-using-rest-api/174 "2018-11-07T07:36:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![legacy\_user](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/legacy_user/32/25_2.png) [@legacy\_user](https://forum.kintone.dev/u/legacy_user)\
**Post date:** [November 7, 2018, 7:36am UTC](https://forum.kintone.dev/t/upload-file-using-rest-api/174/1 "2018-11-07T07:36:00Z")

</div>

Hi developer team,

I’m using REST API to upload some files. (C#)

[https://developer.kintone.io/hc/en-us/articles/212494448-Upload-File](https://developer.kintone.io/hc/en-us/articles/212494448-Upload-File)

Example:

abc.pdf

abc.eml

abc.msg

They are rejected. I try to upload by GUI that is OK.

How can I upload that files using REST API?

Thanks,

AnVH

---

<div class="post-metadata">

**Author:** ![legacy\_user](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/legacy_user/32/25_2.png) [@legacy\_user](https://forum.kintone.dev/u/legacy_user)\
**Post date:** [November 7, 2018, 3:12pm UTC](https://forum.kintone.dev/t/upload-file-using-rest-api/174/2 "2018-11-07T15:12:40Z")

</div>

Hello An Vo Hoang,

Can you please provide any error message upon running the script and the actual script that you are using?

Thank you

---

<div class="post-metadata">

**Author:** ![legacy\_user](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/legacy_user/32/25_2.png) [@legacy\_user](https://forum.kintone.dev/u/legacy_user)\
**Post date:** [November 8, 2018, 4:16am UTC](https://forum.kintone.dev/t/upload-file-using-rest-api/174/3 "2018-11-08T04:16:13Z")

</div>

Hello&nbsp;Yuzo Arai,

I’m using C# script:

```
static async Task UploadFile(string filePath)
{
    var fileName = filePath.Split('\\').Last();
    MultipartFormDataContent form = new MultipartFormDataContent();
    using (var stream = File.OpenRead(filePath))
    {
        byte[] fileBytes = new byte[stream.Length];
        stream.Read(fileBytes, 0, fileBytes.Length);
        form.Add(new ByteArrayContent(fileBytes), "file", fileName);
    }

    try
    {
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Add("X-Cybozu-Authorization", "BASE64 encode");
        client.BaseAddress = new Uri("https://orgId.kintone.com");
        HttpResponseMessage task = await client.PostAsync("k/v1/file.json", form);
        var result = await task.Content.ReadAsStringAsync();
        Console.WriteLine(result);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

```

I received HttpResponse:

- HttpStatus message:&nbsp;Response status code does not indicate success: 520 (520).
- result:&nbsp;{“code”:“GAIA\_IC01”,“id”:“J2kI3TVcdXIIRlmcaDoN”,“message”:“Content-type is required.”}

What’s Content-type that is required when I upload (\*.pdf, \*eml, \*.msg) file?

I also upload other files such as \*.png, \*.jpg, \*.bmp, \*.doc, \*.docx.&nbsp;I don’t input Content-type but it was uploaded successfully.

Thank you

&nbsp;

---

<div class="post-metadata">

**Author:** ![legacy\_user](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/legacy_user/32/25_2.png) [@legacy\_user](https://forum.kintone.dev/u/legacy_user)\
**Post date:** [November 14, 2018, 4:27pm UTC](https://forum.kintone.dev/t/upload-file-using-rest-api/174/4 "2018-11-14T16:27:21Z")

</div>

Hello,

With your C# coding, I got the same error to upload the files with the extension of \*.pdf, \*eml, or \*.msg due to the missing content-type.  
The files with the extension of&nbsp; \*.png, \*.jpg, \*.bmp, \*.doc, or \*.docx. do not seem to require the content-type.

For PDF file, as the media type of the content-type, “application/pdf” should be used.

Another important thing is the way to specify the content-type in your coding. I rewrote the coding, so I was able to upload the pdf file successfully. Please see below. Thanks.

```
   var fileName = filePath.Split('\\').Last();

            MultipartFormDataContent form = new MultipartFormDataContent();Read);

            var fileContent = new StreamContent(File.OpenRead(filePath));

            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");

            fileContent.Headers.ContentDisposition.Name = "file";

            fileContent.Headers.ContentDisposition.FileName = fileName;

            fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

            form.Add(fileContent);

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Add("X-Cybozu-Authorization", "secret");

            HttpResponseMessage task = await client.PostAsync("https://xxx.kintone.com/k/v1/file.json", form);

            var result = await task.Content.ReadAsStringAsync();

            return result;
```

---

<div class="post-metadata">

**Author:** ![legacy\_user](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.kintone.dev/legacy_user/32/25_2.png) [@legacy\_user](https://forum.kintone.dev/u/legacy_user)\
**Post date:** [November 16, 2018, 10:01am UTC](https://forum.kintone.dev/t/upload-file-using-rest-api/174/5 "2018-11-16T10:01:02Z")

</div>

Hello&nbsp;Junko Werner,

I got it.&nbsp;Thanks for your response.

&nbsp;
