Created
August 24, 2018 02:48
-
-
Save int128/fcffb59acf6c23c55a3a615696287fa2 to your computer and use it in GitHub Desktop.
HTTPS client with CA certificate in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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