resty
resty copied to clipboard
How to use http2 in resty
how to use http2.0 in resty? go version : 1.15
HTTP/2 works out of the box with Go 1.16+. Prior to that you need to use a custom transport from golang.org/x/net/http2.
I'm not able to force an HTTP/2 connection with go-resty on go 1.19.4. Here is a snippet of code:
client := resty.New()
client.SetDebug(true)
_, err := client.R().
SetQueryParams(map[string]string{
"q": "test",
"type": "link",
"sort": "updated",
}).
SetHeaders(map[string]string{
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
}).
Get("https://www.reddit.com/search.json")
And the headers:
2023/01/08 18:33:57.116547 DEBUG RESTY
==============================================================================
~~~ REQUEST ~~~
GET /search.json?q=dokku&sort=updated&type=link HTTP/1.1
HOST : www.reddit.com
HEADERS:
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
BODY :
***** NO CONTENT *****
------------------------------------------------------------------------------
Curl seems to default to negotiating an http 2 connection by default. Not sure if there is a way to do that with go-resty.
@culionbear As @moorereason already provided the info above. Golang http client negotiates the HTTP/2 of server supports; otherwise, it falls back to HTTP/1.1; Resty is built on top of the go http client.