使用 node-libcurl returns 发出 curl 请求错误的身份验证错误响应
Making curl request with node-libcurl returns bad authentication error response
我正在尝试使用 node-libcurl 向 checkr api 发出 POST 请求。我需要创建背景调查邀请并将其发送给候选人,但我收到了他们 api 的 Bad authentication error
回复。有帮助吗?
server.js
const data = {
'candidate_id': 'someid',
'package': 'driver_pro',
};
var checkr_sk = 'my_secret_key';
const Curl = require( 'node-libcurl' ).Curl;
curl = new Curl();
curl.setOpt(Curl.option.URL, `https://api.checkr.com/v1/invitations/${checkr_sk}`);
curl.setOpt('FOLLOWLOCATION', true);
curl.setOpt(Curl.option.POST, true);
curl.setOpt(Curl.option.HTTPHEADER, ['Content-Type: application/json']);
curl.setOpt(Curl.option.POSTFIELDS, JSON.stringify(data));
curl.on('end', function (statusCode, body, headers) {
var result = JSON.parse(body);
console.info(statusCode);
console.info(headers);
console.info(body);
console.info(this.getInfo(Curl.info.TOTAL_TIME));
this.close();
});
curl.on('error', function (err, curlErrorCode) {
console.error(err);
console.error(curlErrorCode);
this.close();
});
curl.perform();
我发现发出 curl 请求的最佳方式是使用 node-fetch
库。首先安装node-fetch using npm. A good resource is this curl converter。您可以在 Github.
上找到他们的存储库
curl with node-fetch
const btoa = require('btoa');
const fetch = require('node-fetch');
const data = {
'candidate_id': 'someid',
'package': 'driver_pro',
};
var checkr_sk = 'my_secret_key';
fetch('https://api.checkr.com/v1/invitations', {
method: 'POST',
headers: {
'Authorization': 'Basic ' + btoa(checkr_sk+':'),
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(function(response){ return response.json(); })
.then(function(data) {
const items = data;
console.log(items)
})
我正在尝试使用 node-libcurl 向 checkr api 发出 POST 请求。我需要创建背景调查邀请并将其发送给候选人,但我收到了他们 api 的 Bad authentication error
回复。有帮助吗?
server.js
const data = {
'candidate_id': 'someid',
'package': 'driver_pro',
};
var checkr_sk = 'my_secret_key';
const Curl = require( 'node-libcurl' ).Curl;
curl = new Curl();
curl.setOpt(Curl.option.URL, `https://api.checkr.com/v1/invitations/${checkr_sk}`);
curl.setOpt('FOLLOWLOCATION', true);
curl.setOpt(Curl.option.POST, true);
curl.setOpt(Curl.option.HTTPHEADER, ['Content-Type: application/json']);
curl.setOpt(Curl.option.POSTFIELDS, JSON.stringify(data));
curl.on('end', function (statusCode, body, headers) {
var result = JSON.parse(body);
console.info(statusCode);
console.info(headers);
console.info(body);
console.info(this.getInfo(Curl.info.TOTAL_TIME));
this.close();
});
curl.on('error', function (err, curlErrorCode) {
console.error(err);
console.error(curlErrorCode);
this.close();
});
curl.perform();
我发现发出 curl 请求的最佳方式是使用 node-fetch
库。首先安装node-fetch using npm. A good resource is this curl converter。您可以在 Github.
curl with node-fetch
const btoa = require('btoa');
const fetch = require('node-fetch');
const data = {
'candidate_id': 'someid',
'package': 'driver_pro',
};
var checkr_sk = 'my_secret_key';
fetch('https://api.checkr.com/v1/invitations', {
method: 'POST',
headers: {
'Authorization': 'Basic ' + btoa(checkr_sk+':'),
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(function(response){ return response.json(); })
.then(function(data) {
const items = data;
console.log(items)
})