Answer the question
In order to leave comments, you need to log in
How to connect to a server using a certificate through Goland?
I need to write a client in Go that connects to the server via ssl + basic auth and receives a response. I have the certificate itself with the .crt extension, as well as files with the csr, key, p12, keystore extensions. These files were sent by the company I want to connect to. I do the following:
// the CertPool wants to add a root as a []byte so we read the file ourselves
caCert, err := ioutil.ReadFile("client-file.crt")
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(caCert)
// LoadX509KeyPair reads files, so we give it the paths
clientCert, err := tls.LoadX509KeyPair("client-file.crt", "file.key")
tlsConfig := tls.Config{
InsecureSkipVerify: false,
RootCAs: pool,
Certificates: []tls.Certificate{clientCert},
}
transport := http.Transport{
TLSClientConfig: &tlsConfig,
}
client := http.Client{
Transport: &transport,
}
req, err := http.NewRequest(
"POST", "url", bytes.NewBuffer(xmlbytes),
)
req.SetBasicAuth("login", "pass")
req.Header.Set("Content-Type", "application/xml")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
respbyte, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
buyresp := str.BuyResponse{}
xml.Unmarshal(respbyte, &buyresp)
fmt.Println(buyresp.AuthCode, buyresp.ClientTransactionID, buyresp.ErrorCode)
Answer the question
In order to leave comments, you need to log in
x509: certificate signed by unknown authority.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question