I'm using node-request and trying to send a file to IBM's HDFS, as per this documentation.
Passing this JSON object to request successfully uploads:
var options = {
method: 'put',
body: 'foo',
headers: {
'Content-Type': 'application/octet-stream',
'Transfer-Encoding': 'chunked'
}
};
And running this CURL command successfully uploads a file as well:
curl -v -X PUT -L -b cookie.jar "https://host:port/webhdfs/v1/tmp/myLargeFile.zip?op=CREATE&data=true" --header "Content-Type:application/octet-stream" --header "Transfer-Encoding:chunked" -T "file.txt"
However, trying to specify a file stream like so:
var options = {
method: 'put',
headers: {
'Content-Type': 'application/octet-stream',
'Transfer-Encoding': 'chunked'
},
multipart : [
{ body: fs.createReadStream(localFile) }
]
};
fails, and I don't know where I'm going wrong. How do I reproduce the '--upload-file' argument from CURL using node-request?
Curl is a way you can hit a URL from your code to get an HTML response from it. In NodeJS/ExpressJS we use “request” module for this purpose. In Simple Words, if we want to access HTML content of any site we pass that URL to request( ) and it returns the HTML source code.
CURL upload file allows you to send data to a remote server. The command-line tool supports web forms integral to every web system. When you execute a CURL file upload [1] for any protocol (HTTP, FTP, SMTP, and others), you transfer data via URLs to and from a server.
How to send a file using Curl? To upload a file, use the -d command-line option and begin data with the @ symbol. If you start the data with @, the rest should be the file's name from which Curl will read the data and send it to the server. Curl will use the file extension to send the correct MIME data type.
Uploading File Using Multer in Nodejs: Multer is the most popular module for uploading files. You can upload files of any kind. Multer can be used to upload single or multiple files. It attaches the files in the request body object for uploading the files into the server.
I threw up an example on Github that works.
The gist is that you need to pipe the file into Request:
fs.createReadStream(filePath).pipe(request.put(putURL,options,function(err, httpsResponse, body){
if ( err ) {
console.log('err', err);
} else {
console.log(body);
}
}));
Still not sure how to pass in a file stream as an option in the options params but this will do for me!
-- UPDATE --
Wow, I feel like an absolute idiot. The option would be file
. Yes, it's that simple.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With