node.js 类型错误:请求路径包含未转义的字符,我该如何解决这个问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31024779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: Request path contains unescaped characters, how can I fix this
提问by Lindokuhle Dumisani Masilela
/*Making http request to the api (Git hub)
create request
parse responce
wrap in a function
*/
var https = require("https");
var username = 'lynndor';
//CREATING AN OBJECT
var options = {
host: 'api.github.com',
path: ' /users/'+ username +'/repos',
method: 'GET'
};
var request = https.request(options, function(responce){
var body = ''
responce.on("data", function(chunk){
body += chunk.toString('utf8')
});
responce.on("end", function(){
console.log("Body", body);
});
});
request.end();
Im trying to create a request to the git hub api, the aim is to get the list repository for the specified you, but i keep getting the above mentioned error, please help
我正在尝试创建对 git hub api 的请求,目的是为指定的您获取列表存储库,但我不断收到上述错误,请帮助
采纳答案by pkd
Your "path" variable contains space
您的“路径”变量包含空格
path: ' /users/'+ username +'/repos',
path: ' /users/'+ username +'/repos',
Instead it should be
相反应该是
path: '/users/'+ username +'/repos',
path: '/users/'+ username +'/repos',
回答by qwabra
for other situation can be helpful
对于其他情况可能会有所帮助
JavaScript encodeURI() Function
var uri = "my test.asp?name=st?le&car=saab";
var res = encodeURI(uri);
回答by NuOne
Use
encodeURIComponent()to encode uriand
decodeURIComponent()to decode uri
使用
encodeURIComponent()来编码URI并
decodeURIComponent()解码uri
Its because there are reserved characters in your uri. You will need to encode uri using inbuilt javascript function encodeURIComponent()
这是因为您的 uri 中有保留字符。您将需要使用内置的 javascript 函数对 uri 进行编码encodeURIComponent()
var options = {
host: 'api.github.com',
path: encodeURIComponent('/users/'+ username +'/repos'),
method: 'GET'
};
to decode encoded uri component you can use decodeURIComponent(url)
解码您可以使用的编码 uri 组件 decodeURIComponent(url)

