Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing a Buffer to .attach #126

Open
mikemorton opened this issue Sep 6, 2019 · 5 comments
Open

Passing a Buffer to .attach #126

mikemorton opened this issue Sep 6, 2019 · 5 comments

Comments

@mikemorton
Copy link

The following works fine for me:

  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:

  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() ?

@Daniyal83
Copy link

Hello. I think I have the same issue when trying to send with content-type: multipart/form-data. Have you solved the problem?

@mikemorton
Copy link
Author

I never really got it working. Since I was just writing some demo-level scratch code I wrote the buffer to a temporary file and sent the temp file using .attach('asdf', fs.createReadStream('V:/Multimedia/pumpkins.jpg')).

@Daniyal83
Copy link

I hope someone will answer and solve the issue.

@huan
Copy link

huan commented Jun 27, 2020

I run into this issue today with a Readable stream.

It seems that we have to use an FS Readable stream for this API method.

It would be great if we could use a Readable stream for this API!

@huan
Copy link

huan commented Jun 28, 2020

It turns out that the unirest are using the NPM module form-data, and the form-data module requires that if it received a stream that not fs.ReadStream, we need to provide the file information by additional.

Example:

form.append( 
  'my_file', 
  stream,
  {
    filename: 'bar.jpg', 
    contentType: 'image/jpeg', 
    knownLength: 19806,
  },
)

See: https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options-

huan added a commit to wechaty/plugin-freshdesk that referenced this issue Jun 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants