Created
January 15, 2015 13:35
-
-
Save wbruno/577d49f51445ed06e968 to your computer and use it in GitHub Desktop.
Consulta ao WebService CEP da Republica Virtual (http://www.republicavirtual.com.br/cep/), por json | exemplo em NodeJS por William Bruno <[email protected]>
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
/** | |
* Consulta ao WebService CEP da Republica Virtual (http://www.republicavirtual.com.br/cep/), por json | |
* exemplo em NodeJS por William Bruno <[email protected]> | |
*/ | |
(function(){ | |
'use strict'; | |
var http = require('http'), | |
cep = '05570060', | |
ws = 'http://cep.republicavirtual.com.br/web_cep.php?cep=' + cep + '&formato=json'; | |
http.get(ws, function(res) { | |
var body = ''; | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
var json = JSON.parse(body); | |
switch(parseInt(json.resultado, 10)) { | |
case 1: | |
console.log('Cidade com logradouro completo:'); | |
console.log('Tipo: \t\t' + json.tipo_logradouro); | |
console.log('Logradouro: \t' + json.logradouro); | |
console.log('Bairro: \t' + json.bairro); | |
console.log('Cidade: \t' + json.cidade); | |
console.log('UF: \t\t' + json.uf); | |
break; | |
case 2: | |
console.log('Cidade com logradouro único:'); | |
console.log('Cidade: \t' + json.cidade); | |
console.log('UF: \t\t' + json.uf); | |
break; | |
default: | |
console.log('Falha ao procurar o CEP ' + cep); | |
break; | |
} | |
}); | |
}).on('error', function(e) { | |
console.log("Got error: ", e); | |
}); | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment