Body mixin

The Request and Response objects both have a body property. This is actually a mixin or a class that implements the body interface. The body contains a data stream with methods to retrieve the contents according to the type.

Each body method reads the stream and converts it into the desired format. The stream is completely read and a promise is returned, resolving to the formatted data.

You have already seen how to use the json() method to read JSON formatted data. There is also the text, blob, formData, and arrayBuffer methods. Each one resolves the body to the corresponding format.

To review how to use JSON formatted data, let's see how to retrieve search results in the Podstr application:

 function fetchSearch(term) { fetch("api/search.json?term=" ...

Get Progressive Web Application Development by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.