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

前端 未结 5 657
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 19:18
/*Making http request to the api (Git hub)
create request
parse responce
wrap in a function
*/
var https = require(\"https\");

var username = \'lynndor\';
//CREATIN         


        
5条回答
  •  旧巷少年郎
    2021-01-03 20:20

    Use encodeURIComponent() to encode uri

    and decodeURIComponent() to decode uri

    Its because there are reserved characters in your uri. You will need to encode uri using inbuilt javascript function encodeURIComponent()

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

    to decode encoded uri component you can use decodeURIComponent(url)

提交回复
热议问题