{"record":{"id":"371c66e7c4a7f1ad","repo":"denoland/deno","slug":"err-unescaped-characters","errorCode":"ERR_UNESCAPED_CHARACTERS","errorMessage":"Request path contains unescaped characters","messagePattern":"Request path contains unescaped characters","errorType":"error_code","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_http_client.js","lineNumber":562,"sourceCode":"  } else if (typeof agent.addRequest !== \"function\") {\n    throw new ERR_INVALID_ARG_TYPE(\n      \"options.agent\",\n      [\"Agent-like Object\", \"undefined\", \"false\"],\n      agent,\n    );\n  }\n  this.agent = agent;\n\n  const protocol = options.protocol || defaultAgent.protocol;\n  let expectedProtocol = defaultAgent.protocol;\n  if (this.agent?.protocol) {\n    expectedProtocol = this.agent.protocol;\n  }\n\n  if (options.path) {\n    const path = String(options.path);\n    if (INVALID_PATH_REGEX.test(path)) {\n      throw new ERR_UNESCAPED_CHARACTERS(\"Request path\");\n    }\n  }\n\n  if (protocol !== expectedProtocol) {\n    throw new ERR_INVALID_PROTOCOL(protocol, expectedProtocol);\n  }\n\n  const defaultPort = options.defaultPort ||\n    (this.agent?.defaultPort);\n\n  const optsWithoutSignal = { __proto__: null, ...options };\n\n  // The `_proxy*` fields are internal transport details set only by the proxy\n  // selection below. A caller must not be able to supply them directly: doing\n  // so would route the request through an arbitrary proxy while bypassing the\n  // target permission check that the proxy branch performs. Strip any that came\n  // in via `options` so only the values computed here are honored.\n  delete optsWithoutSignal._proxy;","sourceCodeStart":544,"sourceCodeEnd":580,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/_http_client.js#L544-L580","documentation":"options.path is tested against /[^!-ÿ]/ before the request is built (same regex as Node). A raw space (U+0020), any control character, or any character above U+00FF in the request path throws ERR_UNESCAPED_CHARACTERS, because such bytes would corrupt the HTTP request line or be rejected by servers.","triggerScenarios":"http.request({ path: '/file name.txt' }); { path: '/search?q=café' } (é is above 0xFF); a newline or tab interpolated into the path from user input or filenames.","commonSituations":"Interpolating unencoded filenames, IDs, or free-text query values into the path; copy-pasting URLs with literal spaces from browsers; unicode search endpoints that forgot URLSearchParams.","solutions":["Encode each dynamic segment: '/' + encodeURIComponent(name)","Build query strings with URLSearchParams so values are percent-encoded automatically","Apply encodeURI(path) as a catch-all: it escapes spaces and non-ASCII while keeping /?#&="],"exampleFix":"// before\nhttp.request({ host, path: `/download/${filename}` }, cb); // filename = 'final report.pdf'\n\n// after\nhttp.request({ host, path: `/download/${encodeURIComponent(filename)}` }, cb);","handlingStrategy":"validation","validationCode":"const INVALID_PATH = /[^\\u0021-\\u00ff]/;\nfunction safePath(p) {\n  return INVALID_PATH.test(p) ? encodeURI(p) : p;\n}\nhttp.request({ host, path: safePath(rawPath) }, cb);","typeGuard":null,"tryCatchPattern":"try { http.request({ host, path }, cb); }\ncatch (err) {\n  if (err.code === 'ERR_UNESCAPED_CHARACTERS') {\n    return http.request({ host, path: encodeURI(path) }, cb);\n  }\n  throw err;\n}","preventionTips":["Encode every dynamic path segment with encodeURIComponent and query values with URLSearchParams","Reject URLs pasted from browsers that contain literal spaces instead of silently sending them"],"tags":["http","url-encoding","request-path","node-compat","deno"],"backgroundTag":"unescaped-request-path","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}