Upload file using REST API

Hi developer team,

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

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

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

Hello 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: Response status code does not indicate success: 520 (520).
  • result: {“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. I don’t input Content-type but it was uploaded successfully.

Thank you

 

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  *.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;

Hello Junko Werner,

I got it. Thanks for your response.