# Using the Fetch API to get HTML response in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Feb 23, 2020, updated Jun 10, 2023. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-fetch-html-response

> A short guide to learn how to get the HTML response using the JavaScript Fetch API.

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:

```javascript
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](https://attacomsian.com/blog/javascript-fetch-api#response-object).
