Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

http.request throws because the path has characters that must be escaped

ErrorERR_UNESCAPED_CHARACTERS

Short answer

Node.js refuses to send a request whose path contains characters that must be escaped, such as a space. The path is written into the HTTP request line as it is. Encode each value with encodeURIComponent, or build the address with the URL and URLSearchParams classes and let them do the escaping.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact path you pass to the request, with quotes around it, so spaces and stray characters show.

    console.log(JSON.stringify(options.path));
  2. 2Match what you see, then jump to its fix

    1. 1The printed path contains a space, for example /search?q=hello worldA user value or name with spaces is pasted into the pathFix
    2. 2Several values are glued together and any one of them can hold odd charactersThe address is assembled by string concatenationFix

What does the error look like?

  • ERR_UNESCAPED_CHARACTERS
  • http.request throws before any request is sent

Cause 1 of 2

A user value or name with spaces is pasted into the path

1Check

The Node.js docs say an exception is thrown when the request path has illegal characters, and that currently spaces are rejected.

node -e "console.log(JSON.stringify('/users/' + 'Ann Lee'))"

2Fix

Encode every dynamic piece before joining it into the path.

const path = '/search?q=' + encodeURIComponent(term);
http.request({ host: 'example.com', path }, (res) => res.resume()).end();

3Verify

Print the path again and send the request.

node -e "console.log('/search?q=' + encodeURIComponent('hello world'))"

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 2

The address is assembled by string concatenation

1Check

Hand-built query strings are easy to get wrong. The URL class percent-encodes invalid characters in the path, and URLSearchParams serializes query values.

node -e "const u=new URL('https://example.org/a b'); console.log(u.pathname)"

2Fix

Build the address with URL, set query values through searchParams, and pass the finished URL to the request.

const url = new URL('https://api.example.com/search');
url.searchParams.set('q', term);
https.get(url, (res) => res.resume());

3Verify

Print url.href before sending.

node -e "const u=new URL('https://api.example.com/search'); u.searchParams.set('q','a b'); console.log(u.href)"

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Never concatenate raw user input into a URL; use URL and searchParams.
  • Log the final URL at debug level so a bad character is visible when the error appears.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.