{"id":"42edae4ec9cbf5b6","repo":"expressjs/express","slug":"content-type-cannot-be-set-to-an-array","errorCode":null,"errorMessage":"Content-Type cannot be set to an Array","messagePattern":"Content-Type cannot be set to an Array","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/response.js","lineNumber":677,"sourceCode":" * the charset if not present using `mime.contentType()`.\n *\n * @param {String|Object} field\n * @param {String|Array} val\n * @return {ServerResponse} for chaining\n * @public\n */\n\nres.set =\nres.header = function header(field, val) {\n  if (arguments.length === 2) {\n    var value = Array.isArray(val)\n      ? val.map(String)\n      : String(val);\n\n    // add charset to content-type\n    if (field.toLowerCase() === 'content-type') {\n      if (Array.isArray(value)) {\n        throw new TypeError('Content-Type cannot be set to an Array');\n      }\n      value = mime.contentType(value)\n    }\n\n    this.setHeader(field, value);\n  } else {\n    for (var key in field) {\n      this.set(key, field[key]);\n    }\n  }\n  return this;\n};\n\n/**\n * Get value for header `field`.\n *\n * @param {String} field\n * @return {String}","sourceCodeStart":659,"sourceCodeEnd":695,"githubUrl":"https://github.com/expressjs/express/blob/a3714473feb3d2908add734d340e7755fd85e0a3/lib/response.js#L659-L695","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","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."],"exampleFix":"// before\nres.append('Content-Type', 'application/json'); // throws if already set\n// after\nres.set('Content-Type', 'application/json');","handlingStrategy":"type-guard","validationCode":"if (Array.isArray(type)) {\n  throw new TypeError('Content-Type must be a single string, got an array');\n}\nres.set('Content-Type', type);","typeGuard":"function isSingleContentType(v) {\n  return typeof v === 'string' && !Array.isArray(v);\n}","tryCatchPattern":null,"preventionTips":["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"],"tags":["express","headers","content-type","type-error"],"analyzedSha":"a3714473feb3d2908add734d340e7755fd85e0a3","analyzedAt":"2026-07-31T18:55:36.968Z","schemaVersion":2}