Skip to content

Instantly share code, notes, and snippets.

@vinifig
Forked from Pompeu/cpf_consulta_api_sus.js
Last active December 5, 2016 02:15
Show Gist options
  • Save vinifig/7b09305129ba70ca723d22a2a7b6c429 to your computer and use it in GitHub Desktop.
Save vinifig/7b09305129ba70ca723d22a2a7b6c429 to your computer and use it in GitHub Desktop.
cpf_consulta_api_sus.js
'use strict';
const http = require('http');
const url = 'http://dabsistemas.saude.gov.br/sistemas/sadab/js/buscar_cpf_dbpessoa.json.php?cpf=';
function request(url){
return new Promise((resolve, reject)=>{
http.get(url+cpf, (res)=>{
let statusCode = res.statusCode;
let error = undefined;
let body = '';
if(statusCode < 200 && statusCode > 299){
return reject(statusCode);
}
res.on('data', data => body += data)
res.on('end', () => {
resolve(JSON.parse(body));
});
});
});
}
module.exports = function(cpf){
return new Promise((resolve, reject)=>{
request(url+cpf)
.then(resolve)
.catch((errorCode) => {
reject({
message: 'Request Failed',
errorCode: errorCode
});
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment