{"id":"82b2c741cb58cd8c","repo":"expressjs/express","slug":"name-must-be-a-string-to-req-get","errorCode":null,"errorMessage":"name must be a string to req.get","messagePattern":"name must be a string to req\\.get","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/request.js","lineNumber":70,"sourceCode":" *\n *     req.get('Something');\n *     // => undefined\n *\n * Aliased as `req.header()`.\n *\n * @param {String} name\n * @return {String}\n * @public\n */\n\nreq.get =\nreq.header = function header(name) {\n  if (!name) {\n    throw new TypeError('name argument is required to req.get');\n  }\n\n  if (typeof name !== 'string') {\n    throw new TypeError('name must be a string to req.get');\n  }\n\n  var lc = name.toLowerCase();\n\n  switch (lc) {\n    case 'referer':\n    case 'referrer':\n      return this.headers.referrer\n        || this.headers.referer;\n    default:\n      return this.headers[lc];\n  }\n};\n\n/**\n * Check if the given `type(s)` is acceptable, returning\n * the best match when true, otherwise `false`, in which\n * case you should respond with 406 \"Not Acceptable\".","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/expressjs/express/blob/a3714473feb3d2908add734d340e7755fd85e0a3/lib/request.js#L52-L88","documentation":"Immediately after the presence check, req.get() at lib/request.js:69-71 validates that the header name is a string, because it calls name.toLowerCase() and indexes this.headers with it. A non-string argument (number, object, array, Symbol) indicates the caller passed the wrong value entirely.","triggerScenarios":"Calling req.get(42), req.get(['accept']), req.get({name: 'accept'}), or req.get(Symbol(...)) — any truthy non-string value. Truthy is required, otherwise error [0] fires first.","commonSituations":"Iterating over a headers config that mixes objects and strings; passing the whole req.headers object instead of a key; TypeScript projects with an any-typed value crossing into the call; accidentally passing an index from a map/forEach callback signature.","solutions":["Pass the header name as a string literal or coerce explicitly: req.get(String(name)).","Fix the call site that passes an object/array — usually you meant a property of that object.","Add a type annotation (string) at the source of the value so the compiler catches it."],"exampleFix":"// before\nheaderList.forEach(req.get, req); // forEach passes (value, index, array)\n// after\nheaderList.forEach(function (name) { values.push(req.get(name)); });","handlingStrategy":"type-guard","validationCode":"if (typeof headerName !== 'string') {\n  throw new TypeError(`header name must be a string, got ${typeof headerName}`);\n}\nconst value = req.get(headerName);","typeGuard":"function isValidHeaderName(name) {\n  return typeof name === 'string' && name.length > 0;\n}","tryCatchPattern":"try {\n  const value = req.get(headerName);\n} catch (err) {\n  if (err instanceof TypeError && /must be a string/.test(err.message)) {\n    // non-string (array, number, object) was passed — normalize at the call site\n  }\n  throw err;\n}","preventionTips":["Coerce or reject non-string inputs at the boundary where header names enter your code (query params, config).","Use TypeScript with @types/express so req.get(name: string) is enforced at compile time.","Watch for accidentally passing arrays (e.g. from qs parsing) where a single string is expected."],"tags":["express","http-headers","typeerror","type-validation"],"analyzedSha":"a3714473feb3d2908add734d340e7755fd85e0a3","analyzedAt":"2026-07-31T18:55:36.968Z","schemaVersion":2}