3

I am trying to make a SOAP call over https.

I have tried using the gosoap["github.com/tiaguinho/gosoap"] library.

func main() {
    if soap, err := gosoap.SoapClient("https://domain:port/path?WSDL"); err != nil {
        fmt.Println(err)
        return
    } else {
        params := gosoap.Params{
            "param1": "abc",
            "param2":   "def",
            "param3":     "123",
        }

        if res, err := soap.Call("MethodName", params); err != nil {
            fmt.Println(err)
            return
        } else {
            fmt.Println(res)
            return
        }
    }
}

Here is the error I am getting:

Get https://domain:port/path?WSDL: x509: certificate signed by unknown authority
  1. How do I send Headers?
  2. How do I resolve the certificate issue?
Eric Fernandes
  • 106
  • 1
  • 6

1 Answers1

0

As Berkant comment said, "this means that the installed CA where your app is running could not confirm the certificate".

The best is check if the certificate of your host is valid and properly configured. If your host use your own certificate, the best is to add it to knows certificates of your system/envinronment. You can also add it to golang itself, or ignore it.

To add your own certificate to golang, see this answer:

How to send a https request with a certificate golang

Then set the client like below:

To ignore it, gosoap.Client has Client field allowing set http.Client, but only after calling gosoap.SoapClient, so you need to mimic gosoap.SoapClient:

func SoapClient(wsdl string) (*gosoap.Client, error) {
    _, err := url.Parse(wsdl)
    if err != nil {
        return nil, err
    }

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify : true},
    }

    //client := &http.Client{Transport: tr}//1: if you wanna add only soap headers
    client := &http.Client{Transport: NewAddHeaderTransport(tr)}//1: if you wanna add http headers

    //2: if you wann add soap headers, soap.Client has HeaderName and HeaderParams field

   soapClient := &gosoap.Client{
        HttpClient:      client,
        WSDL:        wsdl,
        URL:         strings.TrimSuffix(d.TargetNamespace, "/"),
    }

    err := getWsdlDefinitions(wsdl, &soapClient.Definitions, client)
    if err != nil {
        return nil, err
    }

    return soapClient, nil
}

//we use interface{} since gosoap.wsdlDefinitions is unexported
func getWsdlDefinitions(u string, wsdl interface{}, client *http.Client) (err error) {
    r, err := client.Get(u)
    if err != nil {
        return err
    }
    defer r.Body.Close()

    decoder := xml.NewDecoder(r.Body)
    decoder.CharsetReader = charset.NewReaderLabel
    return decoder.Decode(wsdl)
}

type AddHeaderTransport struct {
    T http.RoundTripper
}

func (adt *AddHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    req.Header.Add("Your-Header", "your header value")
    return adt.T.RoundTrip(req)
}

func NewAddHeaderTransport(T http.RoundTripper) *AddHeaderTransport {
    if T == nil {
        T = http.DefaultTransport
    }
    return &AddHeaderTransport{T}
}
gstvg
  • 889
  • 4
  • 10