Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 235
Razvi Certezeanu Avatar asked Jul 11 '16 00:07

Razvi Certezeanu


People also ask

What is frame in http2?

Frames. Frames are the new basic unit of communication in HTTP/2 and replace the familiar header & body format of current HTTP requests/responses. Instead, each piece of communication between the client and server is packed up into a binary “frame” before it is sent over the connection.

What is http2 cleartext?

HTTP2 Over Cleartext (H2C) However, H2C or “http2 over cleartext” is where a normal transient http connection is upgraded to a persistent connection that uses the http2 binary protocol to communicate continuously instead of for one request using the plaintext http protocol.

What is HTTP H2C?

H2c is established protocol shorthand for HTTP/2 initiated by a HTTP/1.1 Upgrade header sent over cleartext communication. The attack occurs when a hacker uses h2c to send requests to an intermediary server (known as a proxy server), which can then evade the server access controls.

What is HTTP transport Golang?

It establishes network connections as needed 40 // and caches them for reuse by subsequent calls. It uses HTTP proxies 41 // as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and 42 // $no_proxy) environment variables.


1 Answers

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))
like image 140
Larry.Z Avatar answered Sep 28 '22 05:09

Larry.Z