Node.js - 使用流并从 Got HTTP 请求库中获取正文返回

问题描述

我正在尝试将 HTTP 请求库从 request 转换为 Got,但是在使用 Got 的流时遇到了问题。我正在尝试使用流(我能够在下面的示例中正常工作),但是在使用流时从我的放置请求中获取返回时遇到了一些问题。

我正在尝试转换:

fs.createReadStream(filepath).pipe(request.put(options,function(err,res,body) {
    if (err) {
        console.log(err)
        return
    }
    console.log(body)
}));

我试过了:

(async () => {
    let res = fs.createReadStream(filepath).pipe(got.stream.put(options));
    // res is a return from createReadStream() not got.stream.put(options)
})();
stream.pipeline(fs.createReadStream(filepath),got.stream.put(options),(err) => {
    // err is an error from stream.pipeline and no return from got.stream.put(options)
    console.log(err);
})
const pipeline = promisify(stream.pipeline);

(async () => {
    await pipeline(
        fs.createReadStream(filepath,got.stream.post(options)
    );
})();

所有这些方法的问题是我无法得到 Got 的回应。如果 request 有一个包含 error,response,and body 的回调,Got 似乎确实有这样做的等效项。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)