Set the CSV MIME content type

How to label CSV correctly with the text/csv media type: what the header changes, which parameters matter, how to set it, and when text/plain fits better.

When a server, script or browser hands over CSV data, the content type tells the receiving software what kind of bytes it is getting. The registered media type for comma-separated values is text/csv. Setting it correctly does not change the data. It changes how clients decide to treat that data: display it, download it, or pass it to another application.

Short answer: is text/csv the right label for your use?

For comma-separated tabular data, use text/csv. If the bytes are UTF-8, add an explicit charset=utf-8 parameter. If you want the file saved rather than shown in the browser, also send a Content-Disposition header with attachment and a filename. The main benefit is that clients and intermediaries can identify the payload as tabular text instead of guessing. The key limitation is that the label is only a description. It does not guarantee a delimiter, encoding, quoting style or header row, so the content itself still has to be well formed. Use text/plain instead when you want people to read the raw text in a browser, for example while debugging, or when the content is not really CSV, such as log output that only happens to contain commas.

What setting the content type actually changes

The content type is metadata. It travels in an HTTP response header, a MIME part of an email, or the type property of a browser Blob. The file's bytes stay exactly the same. Characters are not reordered, substituted or restyled, and nothing about the visual presentation is applied. This is different from a file extension. The .csv extension is a naming convention on disk. For an HTTP response, the Content-Type header is what tells the client what it received, and browsers generally give it priority over the extension in the URL.

HTTP/1.1 200 OK
Content-Type: text/csv; charset=utf-8
Content-Disposition: attachment; filename="report.csv"
X-Content-Type-Options: nosniff

RFC 4180, which registered text/csv, defines two optional parameters. The first is charset, which declares the character encoding. The second is header, with the value present or absent, which says whether the first line holds column names. Declare charset explicitly rather than leaving the receiver to guess. You can send the header parameter, but many consumers ignore it, so do not rely on it to control parsing.

CSV, TSV and types that are often confused with them

Format | Media type | Notes
Comma-separated values | text/csv | Registered type for CSV; the specification assumes commas as delimiters
Tab-separated values | text/tab-separated-values | Registered type for tab-delimited text
Raw text for viewing | text/plain | Useful when you want inline, human-readable display instead of spreadsheet handling
Excel workbook files | application/vnd.ms-excel and related types | Meant for spreadsheet workbook formats, not plain CSV
Unknown binary data | application/octet-stream | Generic fallback; tells the client nothing about the structure

Some systems label CSV as application/vnd.ms-excel to push it toward a spreadsheet application. This mislabels the data. A client may try to treat the file as a workbook format, or it may show warnings because the content does not match the declared type. For the same reason, avoid sending semicolon-delimited or tab-delimited data as text/csv. The media type cannot communicate a different delimiter, and a receiver that follows the specification will expect commas.

How to set it in common environments

Apache, using a directive in the server configuration or an .htaccess file where overrides are allowed:

AddType text/csv .csv
AddCharset utf-8 .csv

nginx: check whether your installed mime.types file already maps csv. If it does not, add the mapping there. A types block placed inside a server or location block replaces the inherited mappings rather than adding to them, so editing the shared file is usually safer.

# in mime.types, inside the existing types { ... } block
text/csv    csv;

# optionally, in the http, server or location context
charset utf-8;
charset_types text/csv;

Python with Flask, returning generated CSV:

from flask import Response

@app.route("/export")
def export():
    data = build_csv()  # returns a str
    return Response(
        data,
        mimetype="text/csv",
        headers={"Content-Disposition": "attachment; filename=export.csv"},
    )

Node.js with Express:

app.get("/export", (req, res) => {
  res.set("Content-Type", "text/csv; charset=utf-8");
  res.attachment("export.csv");
  res.send(buildCsv());
});

In the browser, when you build a downloadable file client-side, set the type on the Blob:

const blob = new Blob([csvString], { type: "text/csv;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "export.csv";
a.click();
URL.revokeObjectURL(url);

After deploying, inspect the actual response headers with your browser's developer tools or a command-line HTTP client. Proxies, CDNs and framework defaults can override what you configured.

Inline display versus download

Browsers do not all handle a text/csv response that has no Content-Disposition header the same way. Some download it and some may show it inline. If you need consistent behavior, set it explicitly. Use Content-Disposition: attachment to download, or inline when display is acceptable. If the goal is a quick readable view in the browser tab, text/plain with inline disposition is the more predictable choice. Keep in mind that it removes the hint that the content is tabular.

Will the file stay readable after downloading or sharing?

Encoding is the most common source of trouble. The charset you declare must match the bytes you actually send. A mismatch produces garbled accented letters, symbols and non-Latin scripts. Also note that the HTTP header is lost once the file is saved to disk. A spreadsheet application opening a local file never sees the declared charset and must detect the encoding itself. Some spreadsheet software looks for a UTF-8 byte order mark at the start of the file to make that decision. Behavior varies by product, version and import method, so test with the specific application your readers use instead of assuming one result applies everywhere. A byte order mark can also cause problems for strict parsers that do not expect it, which makes it a tradeoff to decide per audience.

Delimiters and line endings matter as well. RFC 4180 describes CRLF line breaks, double-quoted fields for values that contain commas, quotes or line breaks, and doubled quotes inside quoted fields. Many parsers also accept LF-only line endings, but strict consumers may not, so match what your receiving system documents. In some regional settings, spreadsheet software expects semicolons. The media type cannot signal that, so either follow the comma convention or document the delimiter for recipients.

Accessibility depends on how the data is opened. When raw CSV is shown as text, assistive technology reads it as a run of values and commas, which is hard to follow for anything beyond a few columns. When it is opened in a spreadsheet, accessibility depends on that application. If people need to read the data on a web page, publish an HTML table with proper header cells, or a plain-language summary, and offer the CSV as a download alongside it rather than as the only format.

Safety notes

Send X-Content-Type-Options: nosniff so browsers respect the declared type instead of sniffing the content. Be aware that a correct content type does not protect recipients from formula injection. Cells beginning with characters such as =, +, - or @ may be evaluated as formulas when the file is opened in a spreadsheet. If your export includes user-supplied values, neutralize or escape such cells in a way that suits the downstream use, and record that choice so analysts know the stored values were altered.

Checklist

  • Use text/csv for comma-delimited data and text/tab-separated-values for tab-delimited data
  • Declare charset explicitly and make sure it matches the actual bytes
  • Add Content-Disposition when you need predictable download or inline behavior
  • Avoid application/vnd.ms-excel and application/octet-stream for plain CSV
  • Quote fields that contain commas, quotes or line breaks, and double any embedded quotes
  • Send X-Content-Type-Options: nosniff
  • Verify the live response headers after deployment
  • Test opening the file in the specific applications your recipients use
  • Provide an accessible HTML table or summary when people need to read the data, not only download it

Gaps in this review

No test results, version-specific documentation or measurements were available for this article. Statements about browsers, spreadsheet software and assistive technology describe general behavior and known sources of variation, not verified results for any named product. Before you rely on a specific outcome, such as automatic encoding detection or inline display, confirm it with the exact software and versions your audience uses.