What is the equivalent of curl --upload-file in node-request
Asked Answered
S

1

9

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?

Sn answered 30/1, 2015 at 22:31 Comment(0)
S
9

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.

Sn answered 2/2, 2015 at 16:18 Comment(1)
Where would you specify a username and password for the remote url?Aerugo

© 2022 - 2024 — McMap. All rights reserved.