使用ReadableStream作为请求主体获取 [英] Fetch with ReadableStream as Request Body

查看:125
本文介绍了使用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.com
Connection: keep-alive
Content-Length: 0
Origin: https://example.com
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: https://example.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

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

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.

注意:这是一个更具体的附带问题,来自:推荐答案

我们正在努力使这项工作有效,请参见

We're working on making this work, see https://github.com/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作为请求主体获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆