使用代理时NodeJS报Request path contains unescaped characters错误
Hey there, let's tackle this frustrating proxy issue you're hitting! Based on the details you shared—working requests without proxy, failing with any proxy, and ruling out encoding/URL/provider/server issues—the problem likely stems from how the older version of request-promise (and its underlying request library) parses proxy URLs with authentication. Here are actionable fixes to try:
1. Manually escape special characters in your proxy URL
If your proxy password (or username) contains special characters like @, :, /, or %, these can break URL parsing in older request versions. Even if you think you've checked encoding, double-check by replacing these characters with their URL-encoded equivalents:
@→%40:→%3A/→%2F%→%25
For example, if your proxy URL is http://user:pass@word@proxyserver:8080, rewrite it to:
http://user:pass%40word@proxyserver:8080
2. Use the proxy configuration object instead of a URL string
Older versions of request handle proxy authentication more reliably when you pass a configuration object instead of a raw URL. This avoids any parsing issues with special characters in credentials. Modify your opts like this:
{ url: "https://google.com", proxy: { host: "proxyserver", port: 8080, // Replace with your actual proxy port auth: { user: "your-proxy-username", pass: "your-proxy-password" } } }
Update your makeRequest function to accept this object format, and the request library will handle the authentication encoding correctly under the hood.
3. Upgrade compatible dependencies (if possible)
You're using Node.js v8.10.0 and request-promise v4.2.2—both are quite old. While Node.js 8 is end-of-life, you can still upgrade to the latest compatible versions of request and request-promise that support Node.js 8. This might fix the URL parsing bug entirely:
npm install request@^2.88.2 request-promise@^4.2.6
These versions are known to work with Node.js 8 and include fixes for proxy URL handling.
Why this happens
When using a proxy, the request library parses the proxy URL to extract host, port, and authentication details. Older versions have a bug where special characters in the authentication segment (username/password) cause incorrect path parsing, triggering the "unescaped characters" error. When not using a proxy, this parsing step is skipped, so your requests work as expected.
内容的提问来源于stack exchange,提问作者user8979341

