forked from radoslav/soap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
84 lines (68 loc) · 1.65 KB
/
client.go
File metadata and controls
84 lines (68 loc) · 1.65 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package soap
import (
"bytes"
"crypto/tls"
"encoding/xml"
"errors"
"io/ioutil"
"net/http"
"regexp"
)
func Request(url string, soapRequest []byte, soapAction string) ([]byte, error) {
buffer := new(bytes.Buffer)
buffer.Write(soapRequest)
req, err := http.NewRequest("POST", url, buffer)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "text/xml;charset=UTF-8")
req.Header.Add("SOAPAction", soapAction)
req.Header.Set("User-Agent", "github.com/radoslav/soap/0.1")
req.Close = true
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{Transport: tr}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
rawbody, err := ioutil.ReadAll(res.Body)
if len(rawbody) == 0 {
return nil, errors.New("Empty response")
}
soapResponse, err := SoapFomMTOM(rawbody)
if err != nil {
return nil, err
}
// test for fault
err = CheckFault(soapResponse)
if err != nil {
return nil, err
}
return soapResponse, nil
}
func SoapFomMTOM(soap []byte) ([]byte, error) {
reg := regexp.MustCompile(`(?ims)<[env:|soap:].+Envelope>`)
s := reg.FindString(string(soap))
if s == "" {
return nil, errors.New("Response without soap envelope")
}
return []byte(s), nil
}
func CheckFault(soapResponse []byte) error {
xmlEnvelope := ResponseEnvelope{}
err := xml.Unmarshal(soapResponse, &xmlEnvelope)
if err != nil {
return err
}
fault := xmlEnvelope.ResponseBodyBody.Fault
if fault.XMLName.Local == "Fault" {
sFault := fault.Code + " | " + fault.String + " | " + fault.Actor + " | " + fault.Detail
return errors.New(sFault)
}
return nil
}