Client http2 support

Does go http client by default supports http2 or do we need special settings for it? I am using Go1.9 .

~Rohan

Starting with Go 1.6, the http package has transparent support for the HTTP/2 protocol when using HTTPS.

https://golang.org/pkg/net/http/

Thanks, I am making a post call over the https, that should be fine ?

Here is sample code with http get, it show HTTP1.1

test.go 

package main

import (
	"crypto/tls"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httputil"
)

const (
	GOOGLE_TEST_URL       = "https://www.google.com/search?q=golang&oq=golang&aqs=chrome..69i57j69i60l2j69i59l3.1966j1j7&ie=UTF-8"
	CONTENT_TYPE = "text/html"
)

func executeHTTP2() {
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	req, err := http.NewRequest("GET", GOOGLE_TEST_URL, nil)

	client := &http.Client{Transport: tr}

	dump, err := httputil.DumpRequest(req, true)
	if err != nil {
		return
	}

	log.Printf("%q", dump)

	resp, err := client.Do(req)

	//resp, err := client.Post(GOOGLE_TEST_URL, CONTENT_TYPE, nil)
	if err != nil {
		log.Println("Error while making https request")
		return
	}

	defer resp.Body.Close()

	_, err = ioutil.ReadAll(resp.Body)
	if err != nil && resp.StatusCode != http.StatusOK {
		log.Println("Failed to parse request boxy")
		return
	}

	log.Println("request success")
}

func main() {
	executeHTTP2()
}

OUTPUT:
2018/01/31 09:52:07 "GET /search?q=golang&oq=golang&aqs=chrome…69i57j69i60l2j69i59l3.1966j1j7&ie=UTF-8 HTTP/1.1\r\nHost: www.google.com\r\n\r\n"
2018/01/31 09:52:08 request success


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.