When using the
fetch() method, the resulting
Response object contains information about the network request and its response, including headers, status codes, and status messages.
To access the response body, the
Response object provides various methods such as
text(), and others. The
text() method can be used specifically to retrieve the response as an HTML string.
Here's an example that demonstrates how to download the Google homepage and display it as an HTML string in the console:
fetch('https://www.google.com') .then(response => response.text()) .then(html => console.log(html)) .catch(error => console.error(error));
By utilizing the
text() method, the response body is returned as a string. For further details on response formats in the Fetch API, you can refer to this article.