Overview
The Content-Type header tells the recipient how to interpret a message body — as HTML, JSON, a file, and so on. Getting content types right is both a correctness and a security matter, affecting how browsers render responses and how servers parse requests. This lesson covers content types.
Learning objectives
- Explain what the Content-Type header does.
- Identify common content types in web apps.
- Connect content-type handling to security issues.
Core concept
Content-Type (a MIME type) declares the format of a body, e.g. text/html, application/json, application/x-www-form-urlencoded, multipart/form-data (file uploads), text/plain, or image/png. It appears on both requests (describing the body sent) and responses (describing the body returned). The browser uses the response Content-Type to decide how to handle the content — critically, whether to render it as HTML (and execute any script) or treat it as inert data.
How it works
If a response is served as text/html, the browser renders it and executes embedded script; if served as text/plain or application/json, it is treated as data. This makes Content-Type security-relevant: serving user-controlled content with an HTML content type can turn stored data into executable script (an XSS vector), and browsers historically MIME-sniffed (guessed the type), which the X-Content-Type-Options: nosniff header disables. On the request side, the server must parse bodies according to their declared type and not be confused by mismatches.
Practical example
An application lets users upload a file and later serves it back. If it returns the file as text/html (or lets the browser sniff it as HTML), a file containing markup/script could execute in a victim's browser. Serving user content with a safe, explicit Content-Type (and nosniff, and often Content-Disposition: attachment for downloads) prevents the browser from treating it as an executable web page — a key defensive measure tied to the file-upload and XSS modules.
Cybersecurity relevance
Content-Type handling connects to several vulnerabilities: XSS (user content rendered as HTML), MIME sniffing attacks (mitigated by nosniff), and file-upload risks (serving uploads with an executable type). Setting correct, explicit content types and X-Content-Type-Options: nosniff is standard hardening. Testers check how the server sets content types on user-influenced responses and whether sniffing protections are present.
Common mistakes
- Serving user-controlled content as
text/html, enabling script execution. - Omitting
X-Content-Type-Options: nosniff, allowing MIME sniffing. - Relying on the client's declared request Content-Type without validating the actual body.
Key takeaways
- Content-Type (MIME type) declares a body's format; the browser uses the response type to decide whether to render/execute content as HTML or treat it as data.
- Serving user content as HTML (or allowing MIME sniffing) can lead to XSS;
X-Content-Type-Options: nosniffdisables sniffing. - Set correct, explicit content types and sniffing protections; validate request bodies rather than trusting the declared type — tied to XSS and file-upload defenses.
Check your understanding
- Why does the Content-Type a server assigns to user-controlled content matter for XSS?
- What does the
X-Content-Type-Options: nosniffheader do, and why is it useful?

