golang-lib/web/request.go

29 lines
531 B
Go
Raw Normal View History

2021-06-01 10:51:35 +02:00
package web
2019-08-07 00:15:50 +02:00
import (
"encoding/json"
"net/http"
"time"
)
2021-07-19 17:59:47 +02:00
// JSONRequest issues a GET request to the specified URL and reads the returned
// JSON into value. See json.Unmarshal for the rules for converting JSON into a
// value.
2019-08-07 00:15:50 +02:00
func JSONRequest(url string, value interface{}) error {
2021-07-19 17:59:47 +02:00
netClient := &http.Client{
2019-08-07 00:15:50 +02:00
Timeout: time.Second * 20,
}
resp, err := netClient.Get(url)
if err != nil {
return err
}
err = json.NewDecoder(resp.Body).Decode(&value)
2021-07-19 17:59:47 +02:00
resp.Body.Close()
2019-08-07 00:15:50 +02:00
if err != nil {
return err
}
return nil
}