微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。