# How to upload file with kintone.proxy.upload?

**URL:** <https://forum.kintone.dev/t/how-to-upload-file-with-kintone-proxy-upload/259>\
**Category:** General\
**Created:** [February 28, 2022, 8:41am UTC](https://forum.kintone.dev/t/how-to-upload-file-with-kintone-proxy-upload/259 "2022-02-28T08:41:47Z")\
**Posts on this page:** 3\
**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:** [February 28, 2022, 8:41am UTC](https://forum.kintone.dev/t/how-to-upload-file-with-kintone-proxy-upload/259/1 "2022-02-28T08:41:47Z")

</div>

```javascript
let url = 'https:// ***********.cybozu.com/k/v1/file.json'
let authentication_dev = btoa(' **********')
let headers = {
    'X-Cybozu-Authorization': authentication_dev,
    'Authorization': `Basic ${authentication_dev}`
};
const array = ['<a id="a"><b id="b">hey!</b></a>']
const blob = new Blob(array, {type : 'text/html'})

kintone.proxy.upload(url, 'POST', headers, {'format': 'RAW', 'value': blob}).then(function(args) {
    /* args[0] -> body(string)
    * args[1] -> status(number)
    * args[2] -> headers(object)
    */
    console.log(args[1], JSON.parse(args[0]), args[2]);
}, function(error) {
    console.log(error); // Display the response body (string) from the proxy API
});

```

The result when i run that code:

```nohighlight
code: "GAIA\_HM02"
id: "wFx1jrtllMOuuo8g6N6m"
message: "アップロードするHTTPリクエストの形式が正しくありません。HTTPリクエストはマルチパート形式である必要があります。"

```

I tried insert `'Content-Type' : 'multipart/form-data'` But the result was not change.

Where is error in my code?

Thanks

---

<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:** [March 2, 2022, 12:44am UTC](https://forum.kintone.dev/t/how-to-upload-file-with-kintone-proxy-upload/259/2 "2022-03-02T00:44:07Z")

</div>

Hello Tan,

The error says, “the HTTP request format you are uploading is incorrect. The HTTP request must be in multi-part format.”

FYI, just adding, “Content-Type’ : 'multipart/form-data”  
does not make the HTTP request multi-part.

The following is an example of uploading files to a different domain using kintone.proxy.upload() that I found:

```
(() => {
  const downloadFile = async ({ fileKey }) => {
    const url = `/k/v1/file.json?fileKey=${fileKey}`;
    const headers = { "X-Requested-With": "XMLHttpRequest" };
    // File download cannot be done by kintone.api()
    const res = await fetch(url, { headers });
    return res.arrayBuffer();
  };

  const stringToArrayBuffer = (string) => {
    const arrayBuffer = new ArrayBuffer(string.length);
    const uint8 = new Uint8Array(arrayBuffer);
    return uint8.map((_, i) => string.charCodeAt(i)).buffer;
  };

  const mergeArrayBuffer = (ab1, ab2) => {
    const uint8 = new Uint8Array(ab1.byteLength + ab2.byteLength);
    uint8.set(new Uint8Array(ab1), 0);
    uint8.set(new Uint8Array(ab2), ab1.byteLength);
    return uint8.buffer;
  };

  const getPostData = ({ boundary, body, filename }) => {
    let headerString = "";
    headerString += `--${boundary}\r`;
    headerString += `Content-Disposition: form-data; name="file"; filename="${filename}"\r`;
    headerString += "Content-Type: application/octet-stream\r";
    headerString += "\r";

    let footerString = "";
    footerString += "\r";
    footerString += `--${boundary}--`;

    const header = stringToArrayBuffer(headerString);
    const footer = stringToArrayBuffer(footerString);

    return [header, body, footer].reduce((ac, cu) => mergeArrayBuffer(ac, cu));
  };

  kintone.events.on("app.record.index.show", async (event) => {
    // Registration destination domain and API token
    const domain = "xxx.cybozu.com";
    const apiToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    // Registration source file information
    const downloadFileKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    const filename = "xxxxx.xxx"; // 例）sample.png

    const boundary = "boundary";
    const body = await downloadFile({ fileKey: downloadFileKey });
    const arrayBuffer = getPostData({ boundary, body, filename });

    const url = `https://${domain}/k/v1/file.json`;
    const params = {
      "X-Cybozu-API-Token": apiToken,
      "Content-Type": `multipart/form-data; boundary=${boundary}`,
    };
    const data = { format: "RAW", value: new Blob([arrayBuffer]) };

    try {
      const res = await kintone.proxy.upload(url, "POST", params, data);
      const { fileKey } = JSON.parse(res[0]);
    } catch (error) {
      console.error(error);
    }

    return event;
  });
})();

```

I hope this helps.

---

<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:** [March 2, 2022, 2:51am UTC](https://forum.kintone.dev/t/how-to-upload-file-with-kintone-proxy-upload/259/3 "2022-03-02T02:51:32Z")

</div>

Thank you so much, Chris.

You always give me good advice and new knownledge.

&nbsp;
