-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcep.go
66 lines (58 loc) · 2.01 KB
/
cep.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"bytes"
"fmt"
"net/http"
"io/ioutil"
"encoding/xml"
"encoding/json"
)
type createEnvelope struct {
Body createBody `xml:"Body"`
}
type createBody struct {
CepResponse create `xml:"consultaCEPResponse"`
}
type create struct {
Return workItem `xml:"return"`
}
type workItem struct {
Bairro string `xml:"bairro" json:"bairro"`
Cep string `xml:"cep" json:"cep"`
Cidade string `xml:"cidade" json:"cidade"`
Complemento string `xml:"complemento" json:"complemento"`
Complemento2 string `xml:"complemento2" json:"complemento2"`
End string `xml:"end" json:"end"`
Id int `xml:"id" json:"id"`
Uf string `xml:"uf" json:"UF"`
}
func toUtf8(iso8859_1_buf []byte) []byte {
buf := make([]rune, len(iso8859_1_buf))
for i, b := range iso8859_1_buf {
buf[i] = rune(b)
}
return []byte(string(buf))
}
func PrintCep(_blob []byte) {
b := createEnvelope{}
xml.Unmarshal(toUtf8(_blob), &b)
data, _ := json.MarshalIndent(b.Body.CepResponse.Return, "", "\t")
fmt.Println(string(data))
}
func BuscaCep(_cep string) (string, []byte){
apiUrl := "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl"
data := `<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+`s</cep></cli:consultaCEP></soapenv:Body></soapenv:Envelope>`
client := &http.Client{}
r, err := http.NewRequest("POST", apiUrl, bytes.NewBufferString(data)); if err != nil {
return "-1 ERROR",[]byte("cannot craft request!")
}
r.Header.Add("Content-Type", "application/soap+xml; charset=iso-8859-1")
r.Header.Add("User-Agent","Dalvik/1.6.0 (Linux; U; Android 4.2.1; LG-P875h Build/JZO34L)")
resp, err := client.Do(r); if err != nil {
return "-2 ERROR",[]byte("cannot send request!")
}
_body, err := ioutil.ReadAll(resp.Body); if err != nil {
return "-3 ERROR", []byte("cannot decode body!")
}
return resp.Status, _body
}