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

前端 未结 4 680
迷失自我
迷失自我 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条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 23:19

    You need to add the Authorization to the options like a header encoded with base64. Like:

    var options = {
        host: 'localhost',
        port: 8000,
        path: '/restricted',
        headers: {
         'Authorization': 'Basic ' + new Buffer(uname + ':' + pword).toString('base64')
       }         
    };
    

提交回复
热议问题