1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package main
- import (
- "crypto/tls"
- "crypto/x509"
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- )
- func main() {
- // Load client cert
- cert, err := tls.LoadX509KeyPair("client.pem", "client-key.pem")
- if err != nil {
- log.Fatal(err)
- }
- // Load CA cert
- caCert, err := ioutil.ReadFile("../certsNkeys/ca.pem")
- if err != nil {
- log.Fatal(err)
- }
- caCertPool := x509.NewCertPool()
- caCertPool.AppendCertsFromPEM(caCert)
- // Setup HTTPS client
- tlsConfig := &tls.Config{
- Certificates: []tls.Certificate{cert},
- RootCAs: caCertPool,
- }
- tlsConfig.BuildNameToCertificate()
- transport := &http.Transport{TLSClientConfig: tlsConfig}
- client := &http.Client{Transport: transport}
- resp, err := client.Get("https://localhost:8080/hello")
- if err != nil {
- fmt.Println(err)
- }
- contents, err := ioutil.ReadAll(resp.Body)
- fmt.Printf("%s\n", string(contents))
- }
|