URL contains unescaped characters that are not allowed
Production Risk
Medium — always encode user-supplied URL components to prevent errors and security issues.
Thrown when a URL passed to an HTTP request contains characters that are not allowed in a URL without percent-encoding. These characters must be encoded before being included in a request path.
- 1Using spaces or special characters in URL paths without encoding them
- 2Building URLs from user input without applying encodeURIComponent()
- 3Copying a URL from a browser address bar that auto-decoded percent-encoded characters
Triggered when the HTTP client validates a request URL and finds raw characters that must be percent-encoded.
const http = require('http');
http.request({
hostname: 'localhost',
path: '/search?q=hello world', // space is not allowed
});expected output
Error [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
Fix 1
Encode query parameters and path segments
WHEN When building URLs from dynamic values
const query = encodeURIComponent('hello world');
http.request({
hostname: 'localhost',
path: `/search?q=${query}`, // encoded: /search?q=hello%20world
});Why this works
encodeURIComponent() percent-encodes characters that are not valid in URL components.
Fix 2
Use the URL API to construct safe URLs
WHEN When building complex URLs
const url = new URL('http://localhost/search');
url.searchParams.set('q', 'hello world');
// url.href is 'http://localhost/search?q=hello+world'
http.request(url);Why this works
The URL API handles encoding automatically when you set searchParams.
const http = require('http');
http.request({
hostname: 'localhost',
path: '/search?q=hello world', // space is not allowed
}); // this triggers ERR_UNESCAPED_CHARACTERStry {
// operation that may throw ERR_UNESCAPED_CHARACTERS
riskyOperation()
} catch (err) {
if (err.code === 'ERR_UNESCAPED_CHARACTERS') {
console.error('ERR_UNESCAPED_CHARACTERS:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_unescaped_characters(...args) {
// validate args here
return performOperation(...args)
}✕ Put raw user input directly into URL paths
Unencoded characters cause this error and may also enable path traversal or injection attacks.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev