Created
December 1, 2017 03:23
-
-
Save edgars/804d1eb87221ac6b320a75f40f336dbc to your computer and use it in GitHub Desktop.
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
import ballerina.net.http; | |
@http:configuration {basePath:"/soap"} | |
service<http> cepservice { | |
endpoint | |
<http:HttpClient> httpConnector{ | |
create http:HttpClient("https://apps.correios.com.br", {}); | |
} | |
http:HttpConnectorError err; | |
@http:resourceConfig { | |
methods:["GET"], | |
path:"/cep/{cep}" | |
} | |
resource consultacep (http:Request req, http:Response res, string cep) { | |
xml soapRequest = xml `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:cli="http://cliente.bean.master.sigep.bsb.correios.com.br/"> | |
<soapenv:Header/> | |
<soapenv:Body> | |
<cli:consultaCEP> | |
<cep>{{cep}}</cep> | |
</cli:consultaCEP> | |
</soapenv:Body> | |
</soapenv:Envelope>`; | |
req.setXmlPayload(soapRequest); | |
http:Response clientResponse = {}; | |
clientResponse, err = httpConnector.post("/SigepMasterJPA/AtendeClienteService/AtendeCliente", req); | |
println("Convertendo XML para JSON "); | |
var xmlRetornavel, _ = <xml> clientResponse.getXmlPayload(); | |
json respostaComoJSON = {"logradouro": xmlRetornavel.toJSON({})["soap:Envelope"]["soap:Body"]["ns2:consultaCEPResponse"]["return"]}; | |
println(respostaComoJSON); | |
res.setJsonPayload(respostaComoJSON); | |
res.send(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment