The Fetch API in JavaScript is a modern and versatile alternative to the traditional XMLHttpRequest (XHR) object for making network requests. It introduces support for promises, which simplifies writing asynchronous code.

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 json(), 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:

  .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.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.