Skip to content

Instantly share code, notes, and snippets.

@int128
Created August 24, 2018 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save int128/fcffb59acf6c23c55a3a615696287fa2 to your computer and use it in GitHub Desktop.
Save int128/fcffb59acf6c23c55a3a615696287fa2 to your computer and use it in GitHub Desktop.
HTTPS client with CA certificate in Go
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"net/http"
)
func main() {
tlsConfig := &tls.Config{
InsecureSkipVerify: false,
RootCAs: x509.NewCertPool(),
}
b, err := ioutil.ReadFile("ca.crt")
if err != nil {
log.Fatalf("Could not read the CA certificate: %s", err)
}
if tlsConfig.RootCAs.AppendCertsFromPEM(b) != true {
log.Fatalf("Could not load the CA certificate: %s", err)
}
client := &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}
r, err := client.Get("https://localhost:9000/")
if err != nil {
log.Fatal(err)
}
log.Printf("StatusCode=%v", r.StatusCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment