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.
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
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
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.Cookiereturnshttp.ErrNoCookiewhen absent.FormValuereturns""for missing keys and ignores parse errors; callParseFormand check its error when it matters.ParseFormreads the body only forPOST,PUTandPATCHwithapplication/x-www-form-urlencoded; forGETonly the query is parsed.r.Formmerges both, with body values first.FormValuealso callsParseMultipartForm, which may write large uploads to temporary files (limit: 32 MB in memory).NewRequestreturns an error for an invalid method or URL.- Headers set via
Header.Setare 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.MaxBytesReaderbefore 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:
RemoteAddris the immediate peer. Behind a proxy, parseX-Forwarded-Foronly if you trust that proxy. - Query parameters:
r.URL.Query()re-parses every call; store the result in a variable. - Path wildcards:
http.ServeMuxpatterns fillPathValue; addSetPathValuein tests.