Developer Toolbox

text/html

text/html tells the browser to parse and render the response as a web page and run its scripts.1 It is the type to send for your pages, and the one never to send for content you did not write.

  • Kind text
  • Extensions .html, .htm
Open in cURL Builder A request with this type is filled in.

Header

Content-Type: text/html; charset=utf-8

At a glance

Parameters
charset. Back it up with <meta charset="utf-8"> in the first 1024 bytes of the document.2
In a browser
Parses and renders the page, and runs its scripts.

With curl

curl -i -H 'Accept: text/html' https://example.com/

With fetch

const res = await fetch('https://example.com/', { headers: { Accept: 'text/html' } });
const html = await res.text();

Common mistakes

  • No charset, wrong letters

    Without a charset in the header or a meta tag in the first 1024 bytes, the browser guesses the encoding, and a wrong guess turns every accented letter into two odd ones.

  • User uploads served as HTML

    A file served as text/html from your domain runs its scripts with your cookies. Serve uploads as application/octet-stream or with Content-Disposition: attachment, ideally from another domain.

  • HTML error pages on JSON APIs

    An error page from a proxy (a 502 Bad Gateway from nginx) arrives as HTML, and response.json() throws on the first <. Check response.ok and the Content-Type before parsing.

Sources


  1. 1 HTML Standard, text/html. html.spec.whatwg.org ↩︎
  2. 2 HTML Standard, Specifying the document's character encoding. html.spec.whatwg.org ↩︎