Blog

The Content-Type Header Your Download Button Ignores (Until It Doesn't)

September 13, 2026Xerat022 min read
Share
The Content-Type Header Your Download Button Ignores (Until It Doesn't)

Content-Type is one of the few HTTP headers where getting it wrong doesn't produce an error — it produces a different, wrong-looking result that's easy to misdiagnose as something else entirely. A PDF served as text/plain doesn't fail; it displays as a page of garbled characters. A ZIP file served without the right type might download with a generic extension a user then has to manually rename. None of this throws an exception anywhere, which is exactly why it survives in production far longer than a bug that actually breaks something loudly.

What the header actually controls

Content-Type tells the client how to interpret the bytes that follow — as HTML to render, JSON to parse, an image to display inline, or a file to hand to another application. Browsers can try to guess the type by sniffing the content itself when the header is missing or generic, but that guessing is inconsistent across browsers, gets explicitly disabled by X-Content-Type-Options: nosniff (a security header most modern APIs correctly set), and was never something to rely on in the first place — it's a compatibility fallback, not a substitute for sending the right value.

Content-Disposition decides display vs. download

A correct Content-Type still doesn't guarantee a "Save As" dialog — that's Content-Disposition's job. Content-Disposition: inline (or no header at all, for a type the browser knows how to render) tries to display the content in the browser tab; Content-Disposition: attachment; filename="report.pdf" forces a download with a specific suggested filename. A backend that serves a PDF with the right Content-Type but no Content-Disposition will often render it inline in the browser instead of downloading it — which is either exactly what was wanted, or a support ticket, depending on which one the product actually needed.

Why this passes every test that only checks the status code

A test suite that asserts response.status_code == 200 and stops there will never catch a wrong Content-Type, because the request still technically succeeded — the server sent 200 OK with a body, just labeled wrong. Catching this needs an assertion on the header itself, and ideally something closer to a real client attempting to use the response the way a real consumer would, not just checking that bytes arrived.

Testing this without wiring up real file storage

MockBase's binary response mode serves an uploaded file — a real PDF, image, or archive up to 300 KB — with its own explicit Content-Type, separate from the JSON and raw-text response modes, specifically so a route can be tested against a real binary payload and a specific, wrong-on-purpose header if that's the scenario under test. Setting up "what does my download code do when the server sends the wrong Content-Type" without a real file server behind it turns a scenario that's normally awkward to reproduce into one line of route config.

X

Xerat02

Building MockBase.