{"id":757,"hash":"628f058854f5a3932b9bf7692b484473ee7b3bf86c7e3cc79951a4a159b9199d","pattern":"Node.js HTTP - TypeError: The header content contains invalid characters","full_message":"const http = require('http');\n\nconst req = http.request({\n  method: 'POST',\n  hostname: 'cloudsso‐test.myco.com',\n  port: 80,\n  path: '/as/token.oauth2',\n  headers: {\n    'Content-Type': 'application/json',\n  },\n  agent: false  // create a new agent just for this one request\n\n}, function (res) {\n\n  res.on('headers', function (h) {\n    console.log('headers => ', h);\n  });\n\n  let data = '';\n\n  res.on('data', function (d) {\n    data += d;\n  });\n\n  res.once('end', function () {\n    console.log('data => ', data);\n  });\n\n});\n\nreq.write(JSON.stringify({\n  client_id: 'xxx',\n  client_secret: 'secret',\n  grant_type: 'refresh_token',\n}));\n\nreq.end();\n\nI run this code, and I get the following error:\n\n_http_outgoing.js:358\n    throw new TypeError('The header content contains invalid characters');\n    ^\n\nTypeError: The header content contains invalid characters\n    at ClientRequest.OutgoingMessage.setHeader (_http_outgoing.js:358:11)\n    at new ClientRequest (_http_client.js:105:12)\n    at Object.exports.request (http.js:31:10)\n    at Object.<anonymous> (/Users/alexamil/WebstormProjects/cisco/cdt-now/test/refresh-token.js:9:18)\n    at Module._compile (module.js:570:32)\n    at Object.Module._extensions..js (module.js:579:10)\n    at Module.load (module.js:487:32)\n    at tryModuleLoad (module.js:446:12)\n    at Function.Module._load (module.js:438:3)\n    at Module.runMain (module.js:604:10)\n\nCannot figure out where this error is coming from. I hear it's for security reasons in newer versions of Node but cannot figure out how to get around it.","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"Straight up, looks like we need to use:\n\n headers: {\n    'content-type': 'application/json',\n  },\n\ninstead of\n\n headers: {\n    'Content-Type': 'application/json',\n  },\n\nthese types of vague error messages make me sad!","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/43194065/node-js-http-typeerror-the-header-content-contains-invalid-characters","votes":18,"created_at":"2026-04-19T04:51:37.707960+00:00","updated_at":"2026-04-19T04:51:37.707960+00:00"}