I am trying with the following code by passing cert.pem, key.pem and ..chain.pem but getting status:500 as the service is not able to read these certificates for https RESTful webservice. I went through all the examples in net and tried but still getting same exception.
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"io"
)
func main() {
UserName := "user"
appName := "AP"
PasswordKey := "key"
ServerName := "server"
Sid := "sid"
// Load client cert
cert, err := tls.LoadX509KeyPair("/cert.pem", "/key.pem")
if err != nil {
log.Fatal(err)
}
// Load CA cert
caCert, err := ioutil.ReadFile("/xyz.chain.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
ok := caCertPool.AppendCertsFromPEM(caCert)
if !ok {
panic("failed to parse root certificate")
}
fmt.Println("CERT: ",cert)
// Setup HTTPS client
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
ServerName: "serverName",
RootCAs: caCertPool,
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: transport}
fmt.Println("CLIENT: ",client)
r := strings.NewReader("{\"userName\" : \"" +UserName+"\",\"appName\" : \""+appName+"\",\"passwordKey\" : \""+PasswordKey+"\",\"serverName\" : \""+ServerName+"\",\"sid\" : \""+Sid+"\"}")
lr := io.Reader(r)
resp, err := client.Post("https://xzytr/password/txt","application/json",lr)
if err != nil {
fmt.Println(err)
}
contents, err := ioutil.ReadAll(resp.Body)
fmt.Printf("%s
", string(contents))
}