Skip to content

text/html MIME Type — Meaning, Usage & Examples

text/html
Extensions: .html, .htm Class: text Spec: WHATWG (IANA registered)
View IANA registration ↗

text/html means: text/html is the media type for HyperText Markup Language documents that browsers parse into a live DOM, applying styles and executing scripts. It is the foundational type of the web: every rendered page is served as text/html, and the subtype signals that the content is an interactive document, not raw text. Text media types carry human-readable character data. Because they are text, the charset parameter matters: without an explicit charset=utf-8, receivers may guess a legacy encoding and mangle non-ASCII characters. Text types are also the ones browsers render or display inline, so the subtype decides whether content is treated as markup, code, or plain prose. You will encounter text/html in contexts such as web pages rendered by browsers into the dom; html email bodies interpreted by mail clients; server-rendered templates and single-page application shells. It is commonly associated with the file extension(s) .html, .htm. Because text/html is executable, injecting untrusted strings without encoding causes cross-site scripting. Escape HTML entities in user data, set a Content-Security-Policy, and never serve user uploads as text/html from your main origin.

Advertisement
Ad slot · ref-top

When you'll see text/html

  • Web pages rendered by browsers into the DOM
  • HTML email bodies interpreted by mail clients
  • Server-rendered templates and single-page application shells

How to handle it

When serving a text type, always declare charset=utf-8 unless the specification fixes another encoding, and escape or sanitize content according to how it will be rendered. Treat text/html as executable: never inject untrusted strings into it without encoding. For text/plain and text/csv, preserve line endings and quoting rules so downstream parsers do not split fields incorrectly, and set Content-Disposition when you want a download instead of inline display. As a final checklist for text types: declare charset=utf-8 everywhere, normalize line endings for cross-platform consumers, escape output for the rendering context to prevent injection, and set Content-Disposition deliberately so browsers either display inline or download rather than guessing.

Notes & gotchas

Because text/html is executable, injecting untrusted strings without encoding causes cross-site scripting. Escape HTML entities in user data, set a Content-Security-Policy, and never serve user uploads as text/html from your main origin.

Examples

HTTP
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 130

<!doctype html>
<html lang="en">
  <head><meta charset="utf-8"><title>Example</title></head>
  <body><h1>Hello</h1></body>
</html>
curl
curl -i -H "Accept: text/html" https://api.example.com/v1/resource   # inspect the returned Content-Type
JavaScript (fetch)
const res = await fetch("/v1/resource", { headers: { Accept: "text/html" } });
// verify res.headers.get("Content-Type") starts with "text/html"
const text = await res.text();   // handle according to the declared type
Python (requests)
import requests

res = requests.get("https://api.example.com/v1/resource",
                   headers={"Accept": "text/html"}, timeout=10)
res.raise_for_status()
# confirm the server declared the expected type
assert res.headers["Content-Type"].startswith("text/html")
text = res.text   # decode/handle according to the type

Frequently asked questions

What is the difference between text/html and application/xhtml+xml?

text/html uses the lenient HTML parser that tolerates malformed markup; application/xhtml+xml uses strict XML parsing where a single error aborts rendering. Most sites use text/html.

Why is charset important for text/html?

Without charset=utf-8 the browser may guess an encoding, mis-rendering non-ASCII text and, in edge cases, enabling encoding-based XSS. Declare it in both the header and a meta tag.

Do I need to specify charset for text types?

Yes — append ; charset=utf-8 unless the type defines its own encoding rules. Without it, receivers may fall back to a legacy encoding and corrupt non-ASCII characters. UTF-8 is the safe default for the modern web.

What is the difference between text/plain and text/html?

text/plain is displayed verbatim with no markup interpretation; text/html is parsed as a document, executing styles and scripts. Serving user content as text/html when you meant plain text is a cross-site scripting risk.

Why do my non-ASCII characters appear corrupted?

The receiver guessed the wrong encoding because no charset was declared. Send charset=utf-8 in the Content-Type, ensure the bytes are actually UTF-8, and for HTML add a matching meta charset tag.

Advertisement
Ad slot · ref-in-article

Try the HTML Entity Encoder

Escape and unescape HTML entities safely.

Open tool →