npm请求同时发送令牌和标头'content-type':'application/json'
问题描述:
我试图通过发送令牌和标头'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.
答
你收到这个错误,因为你从来没有定义任何地方需要在你的客户端。如果你想使用在客户端上需要看看这个http://requirejs.org/http://requirejs.org/docs/release/2.2.0/minified/require.js
:
添加到您的项目。
答
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);
});