# Post xls file to commcare from buffer

**URL:** <https://forum.dimagi.com/t/post-xls-file-to-commcare-from-buffer/8055>\
**Category:** Developers\
**Created:** [February 25, 2021, 10:31pm UTC](https://forum.dimagi.com/t/post-xls-file-to-commcare-from-buffer/8055 "2021-02-25T22:31:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mamdoulcisse](https://dimagi.b-cdn.net/user_avatar/forum.dimagi.com/mamdoulcisse/32/1470_2.png) [@mamdoulcisse](https://forum.dimagi.com/u/mamdoulcisse)\
**Post date:** [February 25, 2021, 10:31pm UTC](https://forum.dimagi.com/t/post-xls-file-to-commcare-from-buffer/8055/1 "2021-02-25T22:31:07Z")

</div>

Hi all,

I'm having some issues sending data to CommCareHQ. The data I am trying to upload is an xls file with the endpoint **bulk\_upload\_api** ([Bulk Upload Case Data - CommCare Public - CommCare Public](https://confluence.dimagi.com/display/commcarepublic/Bulk+Upload+Case+Data)) using **axios**. For some reason, I don't have to store the file in the filesystem, but I'm sending the streams (build from a buffer using the npm package **xlsx** ).  
The response I'm having from CommCare is this line:

> ```
> data: {
> code: 500,
> message: "Error: Invalid POST request. Both 'file' and 'case_type' are required"
> }
> 
> ```

...even though I append the file (buffer) and the case\_type. Check snippet below:

> ```
> let data = new FormData();
> data.append('file', buffer);
> data.append('case_type', 'xxx');
> data.append('search_field', 'external_id');
> data.append('create_new_cases', 'on');
> 
> ```

Anyone has evey tried this way of uploading case and could help?

---

<div class="post-metadata">

**Author:** ![Taylor\_Downs1](https://dimagi.b-cdn.net/user_avatar/forum.dimagi.com/taylor_downs1/32/1350_2.png) [@Taylor\_Downs1](https://forum.dimagi.com/u/Taylor_Downs1)\
**Post date:** [February 26, 2021, 6:35am UTC](https://forum.dimagi.com/t/post-xls-file-to-commcare-from-buffer/8055/2 "2021-02-26T06:35:35Z")

</div>

Some additional context that's important here:

1. We're using NodeJs (via OpenFn) and have tested [request](https://www.npmjs.com/package/request), [axios](https://www.npmjs.com/package/axios), and the underlying `http` module using [form-data](https://www.npmjs.com/package/form-data). None of them seem to work, though we can replicate the exact output of a successful file upload (see point 2) when testing on a mock server.
2. We're able to upload using `curl` and a locally stored file, but when doing so via NodeJs and axios we a very strange `405`. When posting to `https://www.commcarehq.org/a/%5Bdomain%5D/importer/excel/bulk_upload_api/` we get a `405` and it's clear that there's some sort of redirect going on that results in a `GET` being executed. In order to get the `500` we need to append the port to the above url: `https://www.commcarehq.org:443/a/%5Bdomain%5D/importer/excel/bulk_upload_api/`. That doesn't seem quite right, but `multipart/form-data` is certainly not my speciality! Is there something we're missing?
3. See images below for what we're sending (when testing on RequestBin). Does this seem like what's being specified in the API?  
 ![image](https://dimagi.b-cdn.net/uploads/default/original/2X/0/03a5aa2c39ed767d2cd00b5d9f91d8d181bbdfe1.png)  
 ![image](https://dimagi.b-cdn.net/uploads/default/original/2X/f/f6ee312b84fb81f5a28f11081a61e401a5d20866.png)

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![czue](https://dimagi.b-cdn.net/user_avatar/forum.dimagi.com/czue/32/493_2.png) [@czue](https://forum.dimagi.com/u/czue)\
**Post date:** [February 26, 2021, 7:03am UTC](https://forum.dimagi.com/t/post-xls-file-to-commcare-from-buffer/8055/3 "2021-02-26T07:03:56Z")

</div>

Here's [the code that triggering the error](https://github.com/dimagi/commcare-hq/blob/master/corehq/apps/case_importer/views.py#L301-L309). My guess is that whatever the JavaScript implementation is doing is causing the data to all land in django's `request.FILES` instead of `request.POST` or vice-versa, but not be properly split across them. **You might want to explore playing with enctype versus content-type headers.** More docs on how Django handles this are [here](https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.FILES) and [here](https://docs.djangoproject.com/en/3.1/topics/http/file-uploads/).

It might be that CommCare should also be more flexible in allowing variants of these requests to be supported. If you are able to find a fix, please report back so we can improve the docs in the meantime!

---

<div class="post-metadata">

**Author:** ![mamdoulcisse](https://dimagi.b-cdn.net/user_avatar/forum.dimagi.com/mamdoulcisse/32/1470_2.png) [@mamdoulcisse](https://forum.dimagi.com/u/mamdoulcisse)\
**Post date:** [March 1, 2021, 9:24am UTC](https://forum.dimagi.com/t/post-xls-file-to-commcare-from-buffer/8055/4 "2021-03-01T09:24:35Z")

</div>

It's all solved Cory. Thanks for digging into this.  
For what it's worth, we should add a filename {filename: 'file.xls=' to the buffer so it could be send as a file.  
`data.append(f'ile', buffer, {filename: 'file.xls'});`
