Skip to content
Advertisement

Retrieve data from a ReadableStream object?

How may I get information from a ReadableStream object?

I am using the Fetch API and I don’t see this to be clear from the documentation.

The body is being returned as a ReadableStream and I would simply like to access a property within this stream. Under Response in the browser dev tools, I appear to have this information organised into properties, in the form of a JavaScript object.

JavaScript

Advertisement

Answer

In order to access the data from a ReadableStream you need to call one of the conversion methods (docs available here).

As an example:

JavaScript

EDIT: If your data return type is not JSON or you don’t want JSON then use text()

As an example:

JavaScript
Advertisement