mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
add a full gRPC example as previously requested at: https://github.com/kataras/iris/issues/1449
Former-commit-id: 0cb5121e7d44644f7f0eb34597ff34274157fe95
This commit is contained in:
50
_examples/mvc/grpc-compatible/http-client/main.go
Normal file
50
_examples/mvc/grpc-compatible/http-client/main.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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())
|
||||
}
|
||||
Reference in New Issue
Block a user