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

Error with "getBuffer()" #439

Closed
charly37 opened this issue Aug 18, 2019 · 8 comments
Closed

Error with "getBuffer()" #439

charly37 opened this issue Aug 18, 2019 · 8 comments
Labels

Comments

@charly37
Copy link

charly37 commented Aug 18, 2019

I tried to use the getBuffer function detail here
https://github.com/form-data/form-data#buffer-getbuffer

to get my from as buffer for axios but i end up with an error. I tried to reproduce with the smallest possible code (the issue is when the getBuffer method is call):

const fs = require('fs');
// To add image to post
var FormData = require('form-data');

var formData = new FormData();
// Fill the formData object
formData.append('dum', 'dum');
// load picture image
formData.append('my_file', fs.createReadStream('/tmp/CatWorried1.jpg'));

var aBuff=formData.getBuffer();

I have the following error when calling this code

[root@cc1b78c1db72 myapp]# npm start

myapp@1.0.0 start /myapp
node index.js

buffer.js:219
throw new ERR_INVALID_ARG_TYPE(
^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object
at Function.from (buffer.js:219:9)
at FormData.getBuffer (/myapp/node_modules/form-data/lib/form_data.js:328:57)
at Object. (/myapp/index.js:12:20)
at Module._compile (internal/modules/cjs/loader.js:777:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:840:10)
at internal/main/run_main_module.js:17:11
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! myapp@1.0.0 start: node index.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the myapp@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2019-08-18T20_32_47_733Z-debug.log

I tried to debug without success and had a look on the existing ticket and only found this one that is related to this function and type #427 but it seems already closed.

I wonder if i miss something or if there a real issue there. any advice to debug would be welcome.

@mapleeit
Copy link
Collaborator

mapleeit commented Aug 20, 2019

https://github.com/form-data/form-data/blob/master/lib/form_data.js#L328

The stream can not be converted to buffer just use Buffer.from.

So is this a bug? @JBtje

A example of RunKit: https://runkit.com/embed/185tn66ybn8p

@mapleeit mapleeit added the bug label Aug 20, 2019
@JBtje
Copy link
Contributor

JBtje commented Aug 20, 2019

createReadStream returns type ReadStream which is an object. Objects cannot be added to buffer hence the error.

If you use readFileSync, it will return a Buffer and it should work.
Question is, is it logical to support a stream when you want to obtains a buffer that you can "hold"?

Example with readFileSync:
https://runkit.com/jbtje/5d5ba44f7c85ae001a1d201f

Note: does not take away that the example is wrong :|

@JBtje
Copy link
Contributor

JBtje commented Aug 20, 2019

@mapleeit see pull request #440 for correction of the documentation.

mapleeit added a commit that referenced this issue Aug 20, 2019
-Fixed: Eerror in the documentations as indicated in #439
@mapleeit
Copy link
Collaborator

Thank you! @JBtje

Understand totally and I'm agree with you.

I'm closing this now. @charly37 feel free to reply if you have any other questions.

@charly37
Copy link
Author

Thx all for the help. Will give it another try ASAP !

@tvk-codecraft
Copy link

createReadStream returns type ReadStream which is an object. Objects cannot be added to buffer hence the error.

If you use readFileSync, it will return a Buffer and it should work.
Question is, is it logical to support a stream when you want to obtains a buffer that you can "hold"?

Example with readFileSync:
https://runkit.com/jbtje/5d5ba44f7c85ae001a1d201f

Note: does not take away that the example is wrong :|

Okay. But how to set content type? Current value is octet/stream.

@JBtje
Copy link
Contributor

JBtje commented Dec 11, 2019

Okay. But how to set content type? Current value is octet/stream.

I'm unsure what you mean...

A Buffer contains raw data. When transferring it, you should use application/octet-stream. Once you have e.g. downloaded/uploaded the data, you can convert it to the type you like.

e.g. if I have the string "HELLO WORLD", then the buffer will contain 0x48 0x45 0x4c 0x4c 0x4f 0x20 0x57 0x4f 0x52 0x4c 0x44. Once this data is transferred, your server (or client) can convert the bytes back to a string representation, or if you like, a decimal number: 87369909750770137432214596.

@tvk-codecraft
Copy link

tvk-codecraft commented Dec 12, 2019

@JBtje, yeah, I am able to understand what you mean. Receiving as application/octet-stream needs additional step to covert the type. Although it can be done, it adds complexity.

Is there anyway, to set the data in FormData along with the content type, as exactly same way it happens with web browsers?

I am using FormData in my test cases to make upload requests for tests. Currently, for now, I added a work around in the server code to check the NODE_ENV & set the content type manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants