-
-
Save vinifig/7b09305129ba70ca723d22a2a7b6c429 to your computer and use it in GitHub Desktop.
cpf_consulta_api_sus.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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