什么是curl --upload-file在node-request中的等价物

时间:2015-01-30 22:31:20

标签: node.js curl node-request

我使用node-request并尝试按照this documentation向IBM的HDFS发送文件。

传递此JSON对象以请求成功上传:

    var options = {
        method: 'put',
        body: 'foo',
        headers: {
            'Content-Type': 'application/octet-stream',
            'Transfer-Encoding': 'chunked'
        }
    };

运行此CURL命令也会成功上传文件:

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"

但是,尝试指定如下的文件流:

    var options = {
        method: 'put',
        headers: {
            'Content-Type': 'application/octet-stream',
            'Transfer-Encoding': 'chunked'
        }, 
        multipart : [
            { body: fs.createReadStream(localFile) }
        ]
    };

失败了,我不知道自己哪里出错了。如何重现' - upload-file'来自CURL的参数使用node-request?

1 个答案:

答案 0 :(得分:6)

我抛出一个有效的example on Github

要点是你需要将文件传递给Request:

fs.createReadStream(filePath).pipe(request.put(putURL,options,function(err, httpsResponse, body){
    if ( err ) {
        console.log('err', err);
    } else {
        console.log(body);
    }
}));

仍然不确定如何传递文件流作为选项参数中的选项,但这对我有用!

- 更新 -

哇,我觉得自己像个白痴。选项为file。是的,就这么简单。