When the server sends the response back to you, it contains two important values:
- The
Content-Type— which you use to decode thebody - The
body— where the meat of the content is
The body contains the data you want. But in order for you to read the data, you need to convert it back into JavaScript using information from the Content-Type header.
Fun Fact: Content-Type can also be called MIME (Multipurpose Internet Mail Extensions) type. You can find out about all the important MIME types here.
Two Important Content Types
For now, you only need to know two content types:
application/json— the response is in JSONtext/plainortext/html— the response is Text
If you use Fetch, you need to convert both content types into JavaScript with their respective transformers
response.jsonfor JSON contentresponse.textfor Text content
fetch('https://link-to-resource') .then(response => response.json()) .then(data => { /* Do something with data*/ })That’s all you need to know about using the response body.
Next, we’ll talk about handling errors.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson