package fetch import ( "fmt" "io" "net/http" "time" ) // Fetch makes a GET request to rawURL, returning the HTML contents of // that webpage as a []byte. An error is also returned. The parameter // timeoutSecs is passed directly to the [http.Client.Timeout] field // of the client making the request. func Fetch(rawURL string, timeoutSecs int) ([]byte, error) { client := http.Client{ Timeout: time.Duration(timeoutSecs) * time.Second, } req, err := http.NewRequest(http.MethodGet, rawURL, nil) if err != nil { return nil, fmt.Errorf("can't create %s request for %s", http.MethodGet, rawURL) } resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("client failed to perform %s request for %s", http.MethodGet, rawURL) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("status for %s for %s: %s", http.MethodGet, rawURL, resp.Status) } htmlBytes, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("can't dump response body into byte slice") } return htmlBytes, nil }