Skip to content

Instantly share code, notes, and snippets.

@salrashid123
Last active September 11, 2017 13:56
Show Gist options
  • Save salrashid123/145078c4ea81957296388063462f1445 to your computer and use it in GitHub Desktop.
Save salrashid123/145078c4ea81957296388063462f1445 to your computer and use it in GitHub Desktop.
Raw http2 gRPC golang client; calls a gRPC endpoint using a plain http2 client
package main
// SEE: https://medium.com/@salmaan.rashid/grpc-with-curl-a65d879a18f7
import (
"bytes"
"crypto/tls"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"golang.org/x/net/http2"
)
func main() {
client := http.Client{
Transport: &http2.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
dat, err := ioutil.ReadFile("frame.bin")
if err != nil {
panic(err)
}
url := "https://main.esodemoapp2.com:50051/echo.EchoServer/SayHello"
fmt.Println("URL:>", url)
req, err := http.NewRequest("POST", url, bytes.NewReader(dat))
if err != nil {
panic(err)
}
req.Header.Set("TE", "trailers")
req.Header.Set("Content-Type", "application/grpc")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println("response Body:", hex.Dump(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment