iris/_examples/mvc/grpc-compatible/http-client/main.go
Gerasimos (Makis) Maropoulos aea836efc7 add a full gRPC example as previously requested at: https://github.com/kataras/iris/issues/1449
Former-commit-id: 0cb5121e7d44644f7f0eb34597ff34274157fe95
2020-03-07 12:53:23 +02:00

51 lines
970 B
Go

package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"io/ioutil"
"log"
"net/http"
pb "github.com/kataras/iris/v12/_examples/mvc/grpc-compatible/helloworld"
)
func main() {
b, err := ioutil.ReadFile("../server.crt")
if err != nil {
log.Fatal(err)
}
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
log.Fatal("credentials: failed to append certificates")
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: cp,
},
}
client := http.Client{Transport: transport}
buf := new(bytes.Buffer)
err = json.NewEncoder(buf).Encode(pb.HelloRequest{Name: "world"})
if err != nil {
log.Fatal(err)
}
resp, err := client.Post("https://localhost/hello", "application/json", buf)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var reply pb.HelloReply
err = json.NewDecoder(resp.Body).Decode(&reply)
if err != nil {
log.Fatal(err)
}
log.Printf("Greeting: %s", reply.GetMessage())
}