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

前端 未结 4 675
迷失自我
迷失自我 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:36

    I suggest to use request module for that, it support wide range functionalities including HTTP Basic Authentication.

    var username = 'username',
        password = 'password',
        url = 'http://' + username + ':' + password + '@some.server.com';
    
    request({url: url}, function (error, response, body) {
       // Do more stuff with 'body' here
    });
    

提交回复
热议问题