Files
NBA_Go/utils/http_client.go
T

29 lines
470 B
Go

package utils
import (
"errors"
"io/ioutil"
"net/http"
"time"
)
func GetJSON(url string) ([]byte, error) {
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New("received non-200 response: " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}