使用 ReadableStream 作为请求正文进行获取

编程入门 行业动态 更新时间:2024-10-26 22:28:45
本文介绍了使用 ReadableStream 作为请求正文进行获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将 fetch 与 ReadableStream.在这个例子中,ReadableStream 应该简单地重复一些数据...".

I'm trying to use fetch with a ReadableStream. In this example, the ReadableStream should simply repeat "Some data..." indefinitely.

fetch('/', { method: 'POST', body: new ReadableStream({ pull: function(controller) { console.log('pull called!'); controller.enqueue('Some data...'); } }) });

这不起作用.pull 执行一次时,请求正文中不会发送任何数据.

This doesn't work. While pull is executed once, no data is sent in the request body.

POST / HTTP/1.1 Host: example Connection: keep-alive Content-Length: 0 Origin: example User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36 Accept: */* Referer: example/ Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8

如何使 ReadableStream(或任何可以写入动态数据的流)可用于 fetch?

How can I make a ReadableStream (or any kind of stream where I can write dynamic data) usable with fetch?

或者,如果这还不可能,您能否指出这一点?谢谢.

Alternatively, if this isn't yet possible, could you please indicate this? Thank you.

注意:这是一个更具体的衍生问题:通过HTTP将数据从浏览器流式传输到服务器的方法

推荐答案

我们正在努力完成这项工作,请参阅 github/whatwg/fetch/pull/425 获取 Fetch 标准的 PR.完成后,您可以期望它(缓慢地)进入浏览器.

We're working on making this work, see github/whatwg/fetch/pull/425 for the PR to the Fetch Standard. Once that is done you can expect this to make its way into browsers (slowly).

更多推荐

使用 ReadableStream 作为请求正文进行获取

本文发布于:2023-10-29 09:35:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正文   ReadableStream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!