Razvi Certezeanu
Razvi Certezeanu

Reputation: 109

How to write/read/send a data Frame using HTTP/2 in Golang?

I was wondering how would one go about writing, reading and sending a Frame, for example a Data Frame, using of course HTTP/2. I know Golang library net/http supports this new protocol, but I do not know how to correctly do the above mentioned aspects.

Thank you in advance!

Upvotes: 7

Views: 2223

Answers (1)

Larry.Z
Larry.Z

Reputation: 3724

try to send http2 request like this

first of all, you need to import http2 package

import "golang.org/x/net/http2"

then, write some request code

t := &http2.Transport{}
c := &http.Client{
    Transport: t,
}
r, _ := http.NewRequest("GET", "https://http2.golang.org/reqinfo", bytes.NewBuffer([]byte("hello")))
resp, err := c.Do(r)
if err != nil {
    fmt.Printf("request error")
}
defer resp.Body.Close()
content, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("body length:%d\n", len(content))

Upvotes: 2

Related Questions