{"record":{"id":"2b8e9de4f902b4de","repo":"nodejs/node","slug":"expected-headers-to-be-an-array","errorCode":null,"errorMessage":"expected headers to be an array","messagePattern":"expected headers to be an array","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"deps/undici/src/lib/core/util.js","lineNumber":545,"sourceCode":"    if (Array.isArray(value)) {\n      for (const entry of value) {\n        rawHeaders.push(Buffer.from(name, 'latin1'), Buffer.from(`${entry}`, 'latin1'))\n      }\n    } else {\n      rawHeaders.push(Buffer.from(name, 'latin1'), Buffer.from(`${value}`, 'latin1'))\n    }\n  }\n\n  return rawHeaders\n}\n\n/**\n * @param {string[]} headers\n * @param {Buffer[]} headers\n */\nfunction encodeRawHeaders (headers) {\n  if (!Array.isArray(headers)) {\n    throw new TypeError('expected headers to be an array')\n  }\n  return headers.map(x => Buffer.from(x))\n}\n\n/**\n * @param {*} buffer\n * @returns {buffer is Buffer}\n */\nfunction isBuffer (buffer) {\n  // See, https://github.com/mcollina/undici/pull/319\n  return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)\n}\n\n/**\n * Asserts that the handler object is a request handler.\n *\n * @param {object} handler\n * @param {string} method","sourceCodeStart":527,"sourceCodeEnd":563,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/undici/src/lib/core/util.js#L527-L563","documentation":"encodeRawHeaders expects a flat array of string field-name/value entries (the raw header list format) and throws TypeError if its argument is not an array. This guards the low-level path that converts raw header arrays into Buffers for the wire.","triggerScenarios":"Calling an internal/raw API (or a diagnostic hook) that ends up in encodeRawHeaders with a non-array: an object, a string, a Map, or undefined.","commonSituations":"Passing a {Name: 'Value'} object where an array of pairs was expected; passing undefined because a headers field was not populated; mismatched undici version where the expected shape changed.","solutions":["Supply headers as a flat array of strings: ['Name', 'Value', 'Name2', 'Value2'].","Convert an object with Object.entries then flatten: Object.entries(h).flat().","Confirm you are on a compatible undici version for the API you are calling."],"exampleFix":"// before\nencodeRawHeaders({ 'content-type': 'application/json' })\n// after\nencodeRawHeaders(['content-type', 'application/json'])","handlingStrategy":"type-guard","validationCode":"function toRawHeaders(h) {\n  if (Array.isArray(h)) return h\n  if (h && typeof h === 'object') return Object.entries(h).flat()\n  throw new TypeError('headers must be an array or a flat key/value object')\n}","typeGuard":"function isRawHeadersArray(h) { return Array.isArray(h) }","tryCatchPattern":null,"preventionTips":["Standardize on the flat-array raw header shape when calling low-level APIs.","Convert header objects via Object.entries(...).flat().","Confirm API/version compatibility before passing headers."],"tags":["headers","validation","undici","typeerror"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}