Skip to content
Tutorials Center
My learning

Topics

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.

Go
resp, err := http.Get(url)
if err != nil { /* network error */ }
defer resp.Body.Close()

Try it

Go GET, status and body

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.

Go Post, PostForm and Head

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

Go
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:

  • err covers failures to complete the exchange: bad URL, DNS failure, refused connection, TLS errors, too many redirects. An HTTP 404 or 500 is not an error; look at StatusCode.
  • When the client gets a redirect response, Get follows up to 10 redirects. If the final response is a redirect without a usable Location, you receive it as is, with a nil error.
  • The error is a *url.Error that wraps the cause, so errors.Is(err, context.DeadlineExceeded) and errors.As work.
  • A non-nil resp.Body must 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.ReadAll or io.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.Get forever. Use &http.Client{Timeout: 10 * time.Second} or NewRequestWithContext.
  • Limit what you read. Wrap resp.Body in io.LimitReader when the server is not trusted.
  • Headers and auth: Get cannot set headers. Build the request with http.NewRequestWithContext, set the headers, and call client.Do.
  • Check the status explicitly: if resp.StatusCode != http.StatusOK { ... }.
  • Do not log full URLs that contain tokens. *url.Error messages include the URL.