expressjs/express · error · TypeError
Content-Type cannot be set to an Array
Error message
Content-Type cannot be set to an Array
What it means
Thrown by `res.set`/`res.header` when the field is `Content-Type` and the value is an array. Content-Type is a singleton header — a response can only have one — and Express also passes the value through `mime.contentType()` to append a charset, which requires a single string.
Source
Thrown at lib/response.js:677
* the charset if not present using `mime.contentType()`.
*
* @param {String|Object} field
* @param {String|Array} val
* @return {ServerResponse} for chaining
* @public
*/
res.set =
res.header = function header(field, val) {
if (arguments.length === 2) {
var value = Array.isArray(val)
? val.map(String)
: String(val);
// add charset to content-type
if (field.toLowerCase() === 'content-type') {
if (Array.isArray(value)) {
throw new TypeError('Content-Type cannot be set to an Array');
}
value = mime.contentType(value)
}
this.setHeader(field, value);
} else {
for (var key in field) {
this.set(key, field[key]);
}
}
return this;
};
/**
* Get value for header `field`.
*
* @param {String} field
* @return {String}View on GitHub ↗ (pinned to a3714473fe)
Solutions
- Set a single string: `res.set('Content-Type', 'application/json')` or use `res.type('json')`.
- Never use res.append for Content-Type — use res.set, which replaces the value.
- When copying headers from an upstream response, join or pick a single value for Content-Type before setting it.
Example fix
// before
res.append('Content-Type', 'application/json'); // throws if already set
// after
res.set('Content-Type', 'application/json'); Defensive patterns
Strategy: type-guard
Validate before calling
if (Array.isArray(type)) {
throw new TypeError('Content-Type must be a single string, got an array');
}
res.set('Content-Type', type); Type guard
function isSingleContentType(v) {
return typeof v === 'string' && !Array.isArray(v);
} Prevention
- Content-Type is a singleton header — never build it via header-merging code that produces arrays
- Check Array.isArray before res.set('Content-Type', ...) when the value comes from config or proxied upstream headers
- When copying headers from another response, special-case Content-Type to take the first value
When it happens
Trigger: `res.set('Content-Type', ['text/html', 'application/json'])`, `res.append('Content-Type', ...)` after a previous value (append concatenates prior and new values into an array and calls res.set), or `res.type()`/`res.set` fed an array-valued variable.
Common situations: Using res.append for Content-Type when a value already exists; copying headers from another response object where values are arrays; forwarding proxied headers wholesale (`res.set(upstream.headers)`) when a header value is an array.
Related errors
- path must be a string to res.sendFile
- Url must be a string
- name argument is required to req.get
- name must be a string to req.get
- app.use() requires a middleware function
AI-assisted analysis of expressjs/express@a3714473fe (2026-07-31).
Data as JSON: /data/errors/42edae4ec9cbf5b6.json.
Report an issue: GitHub ↗.