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

the*_*ott 7 curl node.js node-request

根据本文档,我正在使用node-request并尝试将文件发送到IBM的HDFS .

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

    var options = {
        method: 'put',
        body: 'foo',
        headers: {
            'Content-Type': 'application/octet-stream',
            'Transfer-Encoding': 'chunked'
        }
    };
Run Code Online (Sandbox Code Playgroud)

运行此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) }
        ]
    };
Run Code Online (Sandbox Code Playgroud)

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

the*_*ott 6

在Github了一个有用例子.

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

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

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

- 更新 -

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