在 Node.js 中使用 Request 时,如果请求路径中包含未转义的特殊字符,就会出现 Request path contains unescaped characters 的报错。解决方法是使用 Node.js 内置的 encodeURI
函数来对路径进行编码。代码示例如下:
const request = require('request')
let url = 'http://www.example.com/path/with spaces'
url = encodeURI(url) // 对路径进行编码
request(url, function (error, response, body) {
// ...
})
这样就可以避免出现 Request path contains unescaped characters 的错误了。