带头文件和身份验证的npm请求

时间:2022-02-26 09:40:33

I am trying to access an API using "request" npm. This API requires header "content-type" and a basic authentication. here is what I've done so far.

我正在尝试使用“请求”npm访问API。此API需要标题“content-type”和基本身份验证。这是我到目前为止所做的。

var request = require('request');
var options = {
  url: 'https://XXX/index.php?/api/V2/get_case/2',
  headers: {
    'content-type': 'application/json'
  },

};
request.get(options, function(error, response, body){
console.log(body);
}

).auth("dummyemail@abc.com","password",false);

upon executing this using Node , I am getting an error that says invalid username and password. I've validated the same API, authentication and header using CURL with the command below and it gave an expected HTTP response.

在使用Node执行此操作时,我收到一条错误,指出无效的用户名和密码。我已经使用下面的命令使用CURL验证了相同的API,身份验证和标头,并且它给出了预期的HTTP响应。

curl -X GET -H "content-type: application/json" -u dummyemail@abc.com:password "https://XXX/index.php?/api/V2/get_case/2"

curl -X GET -H“content-type:application / json”-u dummyemail@abc.com:password"https://XXX/index.php?/ api / V2 / get_case / 2“

Please suggest the right way to code request with auth and header.

请建议使用auth和header编码请求的正确方法。


Here is my update code

这是我的更新代码

var auth = new Buffer("dummy@gmail.com" + ':' + "password").toString('base64');
     var req = {
                    host: 'https://URL',
                    path: 'index.php?/api/v2/get_case/2',
                    method: 'GET',
                    headers: {
                        Authorization: 'Basic ' + auth,
                        'Content-Type': 'application/json'
                              }
                };
    request(req,callback);
    function callback(error, response, body) {
      console.log(body);
    }

I am seeing 'undefined' in my console. can you help me here?

我在控制台中看到“未定义”。你能帮帮我吗?

2 个解决方案

#1


15  

Here is how it worked for me

这是它对我有用的方式

 var auth = new Buffer(user + ':' + pass).toString('base64');
 var req = {
     host: 'https://XXX/index.php?/api/V2/get_case/2',
     path: path,
     method: 'GET',
     headers: {
         Authorization: 'Basic ' + auth,
         'Content-Type': 'application/json'
     }
 };

#2


0  

request(url, {
  method: "POST",
  auth: {
    user: this.user,
    pass: this.pass
  }
}, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log('body:', body);
    } else {
      console.log('error', error, response && response.statusCode);
    }
});

#1


15  

Here is how it worked for me

这是它对我有用的方式

 var auth = new Buffer(user + ':' + pass).toString('base64');
 var req = {
     host: 'https://XXX/index.php?/api/V2/get_case/2',
     path: path,
     method: 'GET',
     headers: {
         Authorization: 'Basic ' + auth,
         'Content-Type': 'application/json'
     }
 };

#2


0  

request(url, {
  method: "POST",
  auth: {
    user: this.user,
    pass: this.pass
  }
}, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log('body:', body);
    } else {
      console.log('error', error, response && response.statusCode);
    }
});