The fetch
specification states that the readable stream Body
contains the Body.bodyUsed
flag which is initially set to false
and then is set to true
with a call of any parsing method.
Here's an example:
fetch('/some/path', (res) => {
// res.body.bodyUsed === false
res.json();
// res.body.bodyUsed === true
});
If you try to call a method like res.json()
or res.text()
once again, an exception is thrown.
The question is: why that behavior is used? Why not to allow parsing that readable stream as many times as one wants? I found no explanation of the matter.
PS. In Chrome (and maybe other browsers), that flag is accessible as res.body.locked
.
Without question, some developers prefer Axios over built-in APIs for its ease of use. But many overestimate the need for such a library. The fetch() API is perfectly capable of reproducing the key features of Axios, and it has the added advantage of being readily available in all modern browsers.
The Response Object returned by a fetch() call contains all the information about the request and the response of the network request.
The question is: why that behavior is used? Why not to allow parsing that readable stream as many times as one wants?
It is possible to read Response.body
more than once by using Response.clone()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With