Fetch with ReadableStream as Request Body
Asked Answered
M

1

31

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...');
    }
  })
});

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

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.

Note: This is a more specific spin-off question from: Method for streaming data from browser to server via HTTP

Meza answered 2/12, 2016 at 19:48 Comment(1)
github.com/whatwg/fetch/issues/439Serried
B
23

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).

Between answered 19/12, 2016 at 12:14 Comment(10)
Thank you. What's the reason that the MDN docs today say a ReadableStream is usable? What uses it today?Meza
It is usable with a Response object in Chrome.Between
Might just be a documentation quirk then, but the MDN docs say a ReadableStream is usable in the request. developer.mozilla.org/en-US/docs/Web/API/RequestMeza
Yeah, that looks like a copy-and-paste error. At least the mention in that note. On the other hand, it should be accurate somewhere next year.Between
Is there any update on this? I seem to have exactly the same result in Chrome (58.0.3029.110, Canary and even Electron 1.6.2) as the person asking the question -- Content-Length: 0, poll is only called once, the connection is open and the request is pending (with only headers at the server end). Firefox (53.0.2 and Nightly 55.0a1) doesn't even have ReadableStream.Cosma
We are still working on some fundamental rearchitecting of Chrome's networking code in order to make this possible, although it is a high priority for us. You can follow along, for Chrome at least, in bugs.chromium.org/p/chromium/issues/detail?id=688906Buoyant
@Buoyant Thank you for your swift reply and the useful information you have provided.Cosma
I submitted an MDN doc modification remove positive indicators for ReadableStream in Request bodies: github.com/mdn/browser-compat-data/pull/1333Bucovina
for those looking, Browsers test on this specific feature can be run here w3c-test.org/fetch/api/basic/request-upload.any.htmlLovettalovich
In case anyone hasn't seen it, there is some discussion going on here: groups.google.com/a/chromium.org/forum/#!topic/…Meza

© 2022 - 2024 — McMap. All rights reserved.