Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Networking and the Web

Go http.Request type

Reference for Go http.Request: Method, URL, Header, Body, Form and PostForm, FormValue, Cookie, PathValue, Context, BasicAuth and building client requests.

http.Request represents one HTTP request. On the server side, the framework creates it and passes it to your handler. On the client side, you build one with http.NewRequestWithContext and hand it to Client.Do.

The most used pieces are the method, the URL (path and query), the headers, the body, and the context. A request is read-only for handlers: if you need to change something, clone it.

Go
func handler(w http.ResponseWriter, r *http.Request) {
	name := r.URL.Query().Get("name")
	fmt.Fprintln(w, r.Method, r.URL.Path, name)
}

Try it

Go Reading the parts of a request

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 Request bodies and building client requests

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
type Request struct {
	Method        string
	URL           *url.URL
	Proto         string
	Header        Header
	Body          io.ReadCloser
	GetBody       func() (io.ReadCloser, error)
	ContentLength int64
	Host          string
	Form          url.Values
	PostForm      url.Values
	MultipartForm *multipart.Form
	RemoteAddr    string
	RequestURI    string
	Pattern       string
	// ... more fields
}

func (r *Request) Context() context.Context
func (r *Request) WithContext(ctx context.Context) *Request
func (r *Request) Clone(ctx context.Context) *Request
func (r *Request) Cookie(name string) (*Cookie, error)
func (r *Request) Cookies() []*Cookie
func (r *Request) AddCookie(c *Cookie)
func (r *Request) FormValue(key string) string
func (r *Request) PostFormValue(key string) string
func (r *Request) ParseForm() error
func (r *Request) ParseMultipartForm(maxMemory int64) error
func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error)
func (r *Request) PathValue(name string) string
func (r *Request) BasicAuth() (username, password string, ok bool)
func (r *Request) SetBasicAuth(username, password string)
func (r *Request) UserAgent() string
func (r *Request) Referer() string
Field Server request Client request
Method GET, POST, ... The method you set. Empty means GET.
URL Path and query only (Host and scheme are usually empty). Absolute URL to call.
Header Canonicalized keys (Content-Type). Use Get/Values. Headers to send.
Body Never nil; closed by the server. Nil means no body.
Host The Host header value. Overrides the URL host for the header.
RemoteAddr ip:port of the peer (the proxy, if any). Unused.

Errors and edge cases:

  • r.Cookie returns http.ErrNoCookie when absent. FormValue returns "" for missing keys and ignores parse errors; call ParseForm and check its error when it matters.
  • ParseForm reads the body only for POST, PUT and PATCH with application/x-www-form-urlencoded; for GET only the query is parsed. r.Form merges both, with body values first.
  • FormValue also calls ParseMultipartForm, which may write large uploads to temporary files (limit: 32 MB in memory).
  • NewRequest returns an error for an invalid method or URL.
  • Headers set via Header.Set are canonicalized; assigning to the map directly with a lower-case key bypasses that.

Usage notes

  • Always pass a context on the client side (NewRequestWithContext) so you can cancel or time out.
  • r.Context() ends when the client disconnects or the handler returns. Pass it to database queries and outgoing calls.
  • Limit bodies with http.MaxBytesReader before decoding: an unbounded body is a memory risk.
  • Body can be read once. Buffer it yourself if more than one piece of code needs the bytes.
  • Client IP: RemoteAddr is the immediate peer. Behind a proxy, parse X-Forwarded-For only if you trust that proxy.
  • Query parameters: r.URL.Query() re-parses every call; store the result in a variable.
  • Path wildcards: http.ServeMux patterns fill PathValue; add SetPathValue in tests.