Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Ruby

Idiom #323 Set HTTP request header

Make an HTTP request with method GET to the URL u, with the request header "accept-encoding: gzip", then store the body of the response in the buffer data.

require 'net/http'
response = Net::HTTP.get_response(u, { "accept-encoding": "gzip" })
data = response.body

Requires Ruby >= 3.0
import "io"
import "net/http"
req, err := http.NewRequest("GET", u, nil)
if err != nil {
	return err
}
req.Header.Set("accept-encoding", "gzip")
res, err := http.DefaultClient.Do(req)
if err != nil {
	return err
}
data, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
	return err
}

http.Request.Header is an exported field

New implementation...