Kong / Kong/unirest-nodejs

Passing a Buffer to .attach

Open
#126 5 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
953
Forks
164
PR merge metrics
No merged PRs in 30d

Description

The following works fine for me:

```.js
unirest.put(`${URL_BASE}/api/v1/Databases/${DBID}/Records/${RECORDID}/Content/${FIELDID}`)
.headers({'Content-Type': 'multipart/form-data'})
.attach('asdf', fs.createReadStream('V:/Multimedia/pumpkins.jpg'))
.auth({user:USERNAME, pass:PASSWORD})
.end(function (res) {
console.log('statusCode:', res && res.statusCode);

console.log('body:', res.body);
});
```

However the next thing I'd like to do do is send large uploads in chunks. I can divide the file into chunks as Buffer objects but sending the buffer to attach does not seem to work for me:

```.js
var bufferStream = new stream.PassThrough();
bufferStream.end(Buffer.from(buffer));

unirest.put(`${URL_BASE}/api/v1/Databases/${DBID}/Records/${RECORDID}/Content/${FIELDID}`)
.headers({'Content-Type': 'multipart/form-data'})
.attach('asdf', bufferStream)
.auth({user:USERNAME, pass:PASSWORD})
.end(function (res) {
console.log('statusCode:', res && res.statusCode);

console.log('body:', res.body);
});
```
Yields the folowing error:
> TypeError [ERR_HTTP_INVALID_HEADER_VALUE]: Invalid value "undefined" for header "Content-Length"
at ClientRequest.setHeader (_http_outgoing.js:473:3)
at FormData. (V:\Work\upload test\node_modules\unirest\node_modules\form-data\lib\form_data.js:321:13)
at V:\Work\upload test\node_modules\unirest\node_modules\form-data\lib\form_data.js:265:7
at V:\Work\upload test\node_modules\async\lib\async.js:251:17
at done (V:\Work\upload test\node_modules\async\lib\async.js:126:15)
at V:\Work\upload test\node_modules\async\lib\async.js:32:16
at V:\Work\upload test\node_modules\async\lib\async.js:248:21
at V:\Work\upload test\node_modules\async\lib\async.js:572:34
at V:\Work\upload test\node_modules\unirest\node_modules\form-data\lib\form_data.js:105:13
at FSReqWrap.oncomplete (fs.js:153:21)

What is the correct way to send a raw stream to `.attach()` ?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at Unirest's .attach() handling and the form-data stack shown in node_modules/form-data/lib/form_data.js. Reproduce the request with a PassThrough containing a Buffer and compare it with the working fs.createReadStream example. Done means a raw stream can be passed to .attach() without producing an undefined Content-Length error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.