2017-04-06 43 views
0

我试图通过发送令牌和标头'content-type': 'application/json'来获得API回应,但是我没有知道我应该把他们放在哪里。npm请求同时发送令牌和标头'content-type':'application/json'

这是到目前为止我的代码:

var request = require('request'); 

request.get('google example url', { 
// 
    'auth': { 
    'bearer': '15252727282' 
    }, 
    "headers": { 
    "Content-Type": "application/json" 
    } 
}, function (error, response, body) { 
    console.log('error:', error); // Print the error if one occurred 
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received 
    console.log('body:', body); 
}); 

这就是我找回我的控制台:

error: null 
statusCode: 401 
body: HTTP Token: Access denied. 

回答

0

OK我就用选项作为第一个参数,并与以下行所做的:

const options = { 
 
    url: 'target url', 
 
    method: 'GET', 
 
    headers: { 
 
     "Authorization": "Token token=12425262", 
 
     "Content-type": "application/json" 
 
     
 
    } 
 
}; 
 

 
request(options, function(err, res, body) { 
 
    let json = JSON.parse(body); 
 
    console.log(json); 
 
});

相关问题