forwardemail / forwardemail/superagent

Posting multipart/form-data

Open
#1,520 4 comments 9 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
16.6k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Hi, I'm trying to test a route that accepts multipart/form-data. The route works via postman, a node script, and in the browser, but I can't seem to get it to work with supertest (who referred me to the superagent docs).

Here's the code that's working in a simple node script:

```js
const form = new FormData()
form.append('entry', fs.createReadStream('./readme.md'))
await axios.post('http://localhost:3001/api/v0/files/add', form, {
headers: form.getHeaders()
})
// success!
```

I've tried numerous different strategies with forming a post via supertest:

```js
test('POST file', done => {
const form = new FormData()
form.append('entry', fs.createReadStream('./readme.md'))
request(app)
.post('/api/v0/files/add')
.type('form')
.attach('entry', form)
.set(form.getHeaders())
.expect(201, done)
})
```

I've also tried the above with different variations, like removing `.type` or `.set(headers)`, still with no luck.

The error seems to be throwing from the `busboy` lib, which is likely due to the multer lib:

```
Unhandled error. (Error: Part terminated early due to unexpected end of multipart data
at node_modules/busboy/node_modules/dicer/lib/Dicer.js:65:36
at processTicksAndRejections (internal/process/task_queues.js:75:11))Error [ERR_UNHANDLED_ERROR]: Unhandled error. (Error: Part terminated early due to unexpected end of multipart data
at node_modules/busboy/node_modules/dicer/lib/Dicer.js:65:36
at processTicksAndRejections (internal/process/task_queues.js:75:11))
at PartStream. (node_modules/busboy/lib/types/multipart.js:278:17)
at node_modules/busboy/node_modules/dicer/lib/Dicer.js:65:22
```

Here's the [issue](https://github.com/visionmedia/supertest/issues/602#issue-502243900) with supertest lib

any ideas what i'm doing wrong? Thanks for any help!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.