Logo

Programming-Idioms

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.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
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
}
use HTTP::Tiny;
my $response = HTTP::Tiny->new->get(
	$u, 
	{ 'accept-encoding' => 'gzip' } 
);

my $data;
if ( $response->{success} ) {
	$data = $response->{content};
} else {
	die sprintf "Failed with status '%s' reason '%s'\n",
		$response->{status}, $response->{reason};
}

import requests
response = requests.get(u, headers={'accept-encoding': 'gzip'})
data = response.content[:]
require 'net/http'
response = Net::HTTP.get_response(u, { "accept-encoding": "gzip" })
data = response.body