Last active
September 11, 2017 13:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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