chore: treat http 204 as empty json

This commit is contained in:
andig 2024-12-07 12:08:36 +01:00
parent 03467ee481
commit b4636ff046

View file

@ -35,21 +35,21 @@ func NewHelper(log *util.Logger) *Helper {
// DoBody executes HTTP request and returns the response body
func (r *Helper) DoBody(req *http.Request) ([]byte, error) {
resp, err := r.Do(req)
var body []byte
if err == nil {
body, err = ReadBody(resp)
if err != nil {
return nil, err
}
return body, err
return ReadBody(resp)
}
// GetBody executes HTTP GET request and returns the response body
func (r *Helper) GetBody(url string) ([]byte, error) {
resp, err := r.Get(url)
var body []byte
if err == nil {
body, err = ReadBody(resp)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return body, err
return r.DoBody(req)
}
// decodeJSON reads HTTP response and decodes JSON body if error is nil
@ -66,19 +66,25 @@ func decodeJSON(resp *http.Response, res interface{}) error {
// It returns a StatusError on response codes other than HTTP 2xx.
func (r *Helper) DoJSON(req *http.Request, res interface{}) error {
resp, err := r.Do(req)
if err == nil {
defer resp.Body.Close()
err = decodeJSON(resp, &res)
if err != nil {
return err
}
return err
defer resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
return nil
}
return decodeJSON(resp, &res)
}
// GetJSON executes HTTP GET request and decodes JSON response.
// It returns a StatusError on response codes other than HTTP 2xx.
func (r *Helper) GetJSON(url string, res interface{}) error {
req, err := New(http.MethodGet, url, nil, AcceptJSON)
if err == nil {
err = r.DoJSON(req, &res)
if err != nil {
return err
}
return err
return r.DoJSON(req, &res)
}