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 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 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

Loading

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

@JBtje 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 :|

Loading

@JBtje
Copy link
Contributor

@JBtje JBtje commented Aug 20, 2019

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

Loading

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

@mapleeit mapleeit commented Aug 20, 2019

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.

Loading

@mapleeit mapleeit closed this Aug 20, 2019
@charly37
Copy link
Author

@charly37 charly37 commented Aug 21, 2019

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

Loading

@tvk-codecraft
Copy link

@tvk-codecraft tvk-codecraft commented Dec 11, 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 :|

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

Loading

@JBtje
Copy link
Contributor

@JBtje 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.

Loading

@tvk-codecraft
Copy link

@tvk-codecraft 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.

Loading

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

Successfully merging a pull request may close this issue.

None yet
4 participants