How to Find MIME Types
Look up MIME (content) types for file extensions and vice versa.
Problem
You need to set the correct Content-Type header for a file or determine what type a file is from its extension.
Prerequisites
- Understanding of HTTP headers
- Knowledge of file types
Steps
-
1. Look up by file extension
Map the file extension to its MIME type: .json → application/json, .html → text/html, .png → image/png, .pdf → application/pdf. Use a comprehensive MIME type database for accurate mapping, as some extensions have multiple registered types.
Input.jsonOutputapplication/json -
2. Handle ambiguous extensions
Some extensions map to multiple MIME types depending on context: .xml can be text/xml or application/xml, .js can be text/javascript or application/javascript. Use the most widely compatible type. For web serving, follow the IANA registry.
Input.xmlOutputapplication/xml (preferred) -
3. Set the Content-Type correctly
When serving files, set the Content-Type header to the correct MIME type. Browsers use this to determine how to handle the response. An incorrect type can cause a file to download instead of display, or render incorrectly. Add charset for text types: text/html; charset=utf-8.
Inputserving a JSON fileOutputContent-Type: application/json; charset=utf-8
Expected result
The correct MIME type for a file extension, or the correct extension for a MIME type.
Common errors
- Wrong Content-Type causes file to download
- Set the correct MIME type. For example, text/html for HTML, application/json for JSON. A wrong type like application/octet-stream causes download.
- Missing charset for text types
- Add charset=utf-8 to text MIME types (text/html, text/plain, application/json). Without it, the browser may guess the encoding incorrectly.
Troubleshooting
- If a file renders as plain text, the Content-Type is likely text/plain instead of the correct type.
- For custom file types, use application/octet-stream as a fallback.
Security limits
- MIME type sniffing can be a security risk. Set X-Content-Type-Options: nosniff to prevent browsers from guessing the type.
Related tools
Related tasks
Ready to run it?