Go Reference ยท Networking and the Web
Go http.Get() function
Reference for Go http.Get, Post, Head and PostForm: send a request, read and close the body, check status codes, and why DefaultClient is risky in production.
http.Get(url) sends an HTTP GET request and returns the response. It is the shortest way to fetch something. http.Post, http.PostForm and http.Head work the same way for other methods.
These functions use http.DefaultClient, which has no timeout. They are fine for scripts, examples and tests, but a service should build its own http.Client and send requests with a context.
resp, err := http.Get(url) if err != nil { /* network error */ } defer resp.Body.Close()
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func Get(url string) (resp *Response, err error) func Head(url string) (resp *Response, err error) func Post(url, contentType string, body io.Reader) (resp *Response, err error) func PostForm(url string, data url.Values) (resp *Response, err error)
| Name | Description |
|---|---|
url |
Absolute URL with a scheme (http or https). |
contentType |
Value of the Content-Type request header. |
body |
Request body. If it also implements io.Closer, the client closes it. |
data |
Form fields; sent as application/x-www-form-urlencoded. |
Returns: a *http.Response and an error. resp.Status, resp.StatusCode, resp.Header and resp.Body are the fields you use most. When err is non-nil, resp is nil.
Errors and edge cases:
errcovers failures to complete the exchange: bad URL, DNS failure, refused connection, TLS errors, too many redirects. An HTTP404or500is not an error; look atStatusCode.- When the client gets a redirect response,
Getfollows up to 10 redirects. If the final response is a redirect without a usableLocation, you receive it as is, with a nil error. - The error is a
*url.Errorthat wraps the cause, soerrors.Is(err, context.DeadlineExceeded)anderrors.Aswork. - A non-nil
resp.Bodymust be closed even if you do not read it. The body is never nil on a successful return.
Usage notes
- Close the body, every time. Use
defer resp.Body.Close()right after the error check. Leaked bodies leak connections. - Read the body fully (
io.ReadAllorio.Copy(io.Discard, ...)) if you want the connection returned to the pool for reuse. - No timeout by default. A server that never answers hangs
http.Getforever. Use&http.Client{Timeout: 10 * time.Second}orNewRequestWithContext. - Limit what you read. Wrap
resp.Bodyinio.LimitReaderwhen the server is not trusted. - Headers and auth:
Getcannot set headers. Build the request withhttp.NewRequestWithContext, set the headers, and callclient.Do. - Check the status explicitly:
if resp.StatusCode != http.StatusOK { ... }. - Do not log full URLs that contain tokens.
*url.Errormessages include the URL.
Related
http.Client,http.Request, net/url, net/http- Lessons: HTTP client, URLs and HTTP basics