denoland/deno · error · TypeError

ERR_UNESCAPED_CHARACTERS

ERR_UNESCAPED_CHARACTERS

Error message

Request path contains unescaped characters

What it means

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.

Solutions

  1. Encode each dynamic segment: '/' + encodeURIComponent(name)
  2. Build query strings with URLSearchParams so values are percent-encoded automatically
  3. Apply encodeURI(path) as a catch-all: it escapes spaces and non-ASCII while keeping /?#&=

Example fix

// before
http.request({ host, path: `/download/${filename}` }, cb); // filename = 'final report.pdf'

// after
http.request({ host, path: `/download/${encodeURIComponent(filename)}` }, cb);
Defensive patterns

Strategy: validation

Validate before calling

const INVALID_PATH = /[^\u0021-\u00ff]/;
function safePath(p) {
  return INVALID_PATH.test(p) ? encodeURI(p) : p;
}
http.request({ host, path: safePath(rawPath) }, cb);

Try / catch

try { http.request({ host, path }, cb); }
catch (err) {
  if (err.code === 'ERR_UNESCAPED_CHARACTERS') {
    return http.request({ host, path: encodeURI(path) }, cb);
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/371c66e7c4a7f1ad. Report an issue: GitHub.

Appendix: source

Thrown at ext/node/polyfills/_http_client.js:562

  } else if (typeof agent.addRequest !== "function") {
    throw new ERR_INVALID_ARG_TYPE(
      "options.agent",
      ["Agent-like Object", "undefined", "false"],
      agent,
    );
  }
  this.agent = agent;

  const protocol = options.protocol || defaultAgent.protocol;
  let expectedProtocol = defaultAgent.protocol;
  if (this.agent?.protocol) {
    expectedProtocol = this.agent.protocol;
  }

  if (options.path) {
    const path = String(options.path);
    if (INVALID_PATH_REGEX.test(path)) {
      throw new ERR_UNESCAPED_CHARACTERS("Request path");
    }
  }

  if (protocol !== expectedProtocol) {
    throw new ERR_INVALID_PROTOCOL(protocol, expectedProtocol);
  }

  const defaultPort = options.defaultPort ||
    (this.agent?.defaultPort);

  const optsWithoutSignal = { __proto__: null, ...options };

  // The `_proxy*` fields are internal transport details set only by the proxy
  // selection below. A caller must not be able to supply them directly: doing
  // so would route the request through an arbitrary proxy while bypassing the
  // target permission check that the proxy branch performs. Strip any that came
  // in via `options` so only the values computed here are honored.
  delete optsWithoutSignal._proxy;

View on GitHub (pinned to 9ad36f7a2c)