TypeError: the request path contains unescaped characters, how can I fix this

/*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(); 

I'm trying to create a request to git hub api, the goal is to get a list repository for the user you specify, but I keep getting the above error, please help

+18
github
source share
3 answers

Your path variable contains a space

path: ' /users/'+ username +'/repos',

Instead, he should be

path: '/users/'+ username +'/repos',

+16
source share

for another situation it may be useful

JavaScript function encodeURI ()

 var uri = "my test.asp?name=stΓ₯le&car=saab"; var res = encodeURI(uri); 
+20
source share

Use encodeURIComponent() to encode URI

and decodeURIComponent() for decoding URIs

This is because there are reserved characters in your URI. You will need to encode the URI using the built-in JavaScript encoding encodeURIComponent()

 var options = { host: 'api.github.com', path: encodeURIComponent('/users/'+ username +'/repos'), method: 'GET' }; 

to decode decoded component decodeURIComponent(url) you can use decodeURIComponent(url)

0
source share

All Articles