Error with "getBuffer()"
See original GitHub issueI 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.<anonymous> (/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 https://github.com/form-data/form-data/issues/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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (3 by maintainers)
Top GitHub Comments
createReadStream
returns typeReadStream
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, 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.