0

我正在尝试使用 node-libcurl向checkr api发出 POST 请求。我需要创建并向候选人发送背景调查邀请,但我Bad authentication error从他们的 api 中得到了回复。有什么帮助吗?

服务器.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();
4

1 回答 1

0

我发现发出 curl 请求的最佳方式是使用node-fetch库。首先使用 npm安装node-fetch 。一个很好的资源是这个curl 转换器。你可以在 Github 上找到他们的仓库。

curl 与 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)
  })
于 2021-08-16T06:32:26.697 回答