-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
56 lines (47 loc) · 1.1 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
// Create a CA certificate pool and add the server cert chain pem file to it
caCert, err := os.ReadFile("cert-chain.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Read the key pair to create certificate
cert, err := tls.LoadX509KeyPair("certs/partner-org.example.crt", "certs/partner-org.example.key")
if err != nil {
log.Fatal(err)
}
// Create a HTTPS client and supply the created CA pool
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{cert},
},
},
}
// Request /zero via the created HTTPS client over port 8443 via GET
r, err := client.Get("https://localhost:8443/zero")
if err != nil {
log.Fatal(err)
}
// Read the response body
defer r.Body.Close()
body, err := io.ReadAll(r.Body)
if err != nil {
_ = r.Body.Close()
log.Fatal(err)
}
// Print the response body to stdout
fmt.Printf("%s\n", body)
}