HTTP Client based on NodeJS: How to authenticate a request?

前端 未结 4 666
迷失自我
迷失自我 2020-12-30 23:01

This is the code I have to make a simple GET request:

var options = {
    host: \'localhost\',
    port: 8000,
    path: \'/restricted\'
};

request = http.g         


        
4条回答
  •  旧时难觅i
    2020-12-30 23:40

    In newer version you can also just add auth parameter (in format username:password, no encoding) to your options:

    var options = {
        host: 'localhost',
        port: 8000,
        path: '/restricted',
        auth: username + ':' + password
    };
    
    request = http.get(options, function(res){
        //...
    });
    

    (NOTE: tested on v0.10.3)

提交回复
热议问题