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-8At 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/htmlfrom your domain runs its scripts with your cookies. Serve uploads asapplication/octet-streamor withContent-Disposition: attachment, ideally from another domain.HTML error pages on JSON APIs
An error page from a proxy (a
502 Bad Gatewayfrom nginx) arrives as HTML, andresponse.json()throws on the first<. Checkresponse.okand theContent-Typebefore parsing.
Sources
- 1 HTML Standard, text/html. html.spec.whatwg.org ↩︎
- 2 HTML Standard, Specifying the document's character encoding. html.spec.whatwg.org ↩︎