Set the correct CSV Content-Type

text/csv is the registered media type for CSV. See how it compares with application/csv and text/plain, which header parameters matter, and how to handle CSV uploads.

Short answer

When you serve or send CSV, use text/csv and state the encoding: text/csv; charset=utf-8. This is the registered media type defined alongside the common CSV format specification (RFC 4180). The value application/csv is not a registered media type. Some libraries and older systems still send or expect it, so an endpoint may need to accept it, but it should not be the value you send. Use text/plain only when you want a browser to show the file as readable text instead of handling it as tabular data.

What the Content-Type header changes and what it does not

Content-Type is a label attached to a message or file part. It tells the receiver how to interpret the bytes. It does not change the bytes. Switching a response from application/csv to text/csv leaves the delimiter, quoting, line endings and character encoding exactly as they were. Converting a tab-separated file to comma-separated is a different kind of operation because it rewrites the stored characters. The two are easy to confuse because both get called 'setting the CSV type'.

The label affects what receiving software decides to do. A browser uses it to choose between displaying and downloading. An API framework uses it to pick a body parser. An upload validator uses it to accept or reject a file. When the label is wrong, the data can be perfectly valid and still be mishandled.

Comparing the values you are likely to see

Value | Status | Typical use | Notes
text/csv | Registered | Default for CSV responses and request bodies | Supports optional charset and header parameters
application/csv | Not registered | Emitted by some legacy systems and client libraries | Tolerate on input if needed; avoid sending it
text/plain | Registered, generic | Showing CSV as readable text in a browser | Receivers get no signal that the content is tabular
text/tab-separated-values | Registered | Tab-delimited (TSV) data | Use this instead of text/csv for TSV files
application/vnd.ms-excel | Registered vendor type for Excel workbooks | Sometimes reported for .csv files during browser uploads | Not an accurate label for CSV; tolerate it on uploads only

Writing the response header

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

The charset parameter is optional in the specification. Stating it explicitly means receivers don't have to guess the encoding. The specification also defines an optional header parameter, with the values present or absent, which says whether the first line holds column names. Few consumers read it, but including it does no harm.

Content-Type: text/csv; charset=utf-8; header=present

If the file is an export meant to be saved, the Content-Type header does not decide whether the browser downloads it. Content-Disposition does that, and it also supplies a filename with the .csv extension. Adding X-Content-Type-Options: nosniff tells browsers to trust the declared type instead of guessing from the content.

Content-Type: text/csv; charset=utf-8
Content-Disposition: attachment; filename="export.csv"
X-Content-Type-Options: nosniff

Endpoints that accept CSV uploads

For an API that takes CSV as a raw request body, clients should send Content-Type: text/csv. An endpoint that enforces strict media types can reply with 415 Unsupported Media Type when the declared type is clearly wrong. Multipart form uploads work differently. Each file part has its own type, and browsers usually derive it from the file extension and the operating system's file-type associations. The same .csv file can therefore arrive as text/csv, application/vnd.ms-excel, application/octet-stream, or with no type at all, depending on the user's machine.

  • Accept a short allowlist of declared types instead of only text/csv, for example text/csv, application/csv, text/plain and application/vnd.ms-excel.
  • Check the file extension as a second signal.
  • Treat the declared type as a hint supplied by the client, not as proof of the content.
  • Validate the content itself: decode it with the expected encoding, parse it, and check that rows have a consistent column count before accepting the file.

Will the file stay usable after download or sharing?

Content-Type only matters while the file is in transit. After the file is saved, most systems identify it by its extension, and the charset you declared is not stored with it. When the file is shared by email or chat, the sender's client sets the attachment type again. Most problems after sharing therefore come from the file's contents, not from the header.

  • Encoding: some versions of Excel are widely reported to misread UTF-8 CSV that has no byte order mark. Adding a UTF-8 BOM is a common workaround, but it can break scripts that don't expect one. No test results were supplied here, so verify the behavior in the applications your readers actually use.
  • Delimiter: in regions where the comma is the decimal separator, spreadsheet software may expect semicolons. The media type does not say which delimiter the file uses.
  • Line endings: the specification uses CRLF. Most parsers also accept LF, but strict consumers may not.
  • Accessibility: in a spreadsheet application, a screen reader can navigate the data as a table. Shown as text/plain in a browser, the same data is read as raw lines full of commas, which is hard to follow. If people need to read the data rather than process it, offer an HTML table or a short summary alongside the file. No specific assistive technology was tested for this article.
  • Formula injection: a cell that starts with =, +, - or @ may run as a formula when the file is opened in a spreadsheet. The Content-Type header does nothing to prevent this. If an export includes untrusted input, neutralize those leading characters before writing the file.

When text/plain is the better choice

text/plain fits small samples, logs or documentation examples that people should be able to read directly in a browser tab without a download. The tradeoff is that tools lose the signal that the content is tabular, so text/plain is a poor choice for APIs, integrations and exports that are meant to be opened in spreadsheet software.

What this review could not verify

No test results, application versions or browser measurements were supplied. The statements about registration status and header parameters come from published specifications. The statements about upload labels and spreadsheet encoding describe widely reported behavior that varies by platform and version. Check them against the clients and applications your users actually rely on.