As per the doc¹: If the returned error is nil, the Response will contain a non-nil Body which the user is expected to close. ... On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when CheckRedirect fails, and even then the returned Response.Body is already closed. It is thus safe to defer res.Body.Close() as soon as err has been determined to be nil. Also, I've dispensed with the separate variable client, since it is not used later. ¹: https://godoc.org/net/http#Client.Do
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package http
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
)
|
|
|
|
// Get perform a request using Request with the GET method
|
|
func Get(url string, contentType string, r io.Reader, w io.Writer) error {
|
|
return Request("GET", url, contentType, r, w)
|
|
}
|
|
|
|
// Post perform a request using Request with the POST method
|
|
func Post(url string, contentType string, r io.Reader, w io.Writer) error {
|
|
return Request("POST", url, contentType, r, w)
|
|
}
|
|
|
|
// Put perform a request using Request with the PUT method
|
|
func Put(url string, contentType string, r io.Reader, w io.Writer) error {
|
|
return Request("PUT", url, contentType, r, w)
|
|
}
|
|
|
|
// Delete perform a request using Request with the DELETE method
|
|
func Delete(url string, contentType string, r io.Reader, w io.Writer) error {
|
|
return Request("DELETE", url, contentType, r, w)
|
|
}
|
|
|
|
var devnull = ioutil.Discard
|
|
|
|
// Request will perform the request to the given url and method sending the
|
|
// content of the given reader as the body and writing all the contents
|
|
// of the response to the given writer. The reader and writer are
|
|
// optional.
|
|
func Request(method string, url string, contentType string, r io.Reader, w io.Writer) error {
|
|
req, err := http.NewRequest(method, url, r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if contentType != "" {
|
|
req.Header.Add("Content-Type", contentType)
|
|
}
|
|
|
|
res, err := new(http.Client).Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
|
return errors.New(GetReason(res))
|
|
}
|
|
|
|
if w == nil {
|
|
_, err = io.Copy(devnull, res.Body)
|
|
} else {
|
|
_, err = io.Copy(w, res.Body)
|
|
}
|
|
|
|
return err
|
|
}
|