You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3745 lines
147 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>http - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package http</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "net/http"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. <dd><a href="index.html#pkg-subdirectories">Subdirectories</a></dd>
  59. </dl>
  60. </div>
  61. <!-- The package's Name is printed as title by the top-level template -->
  62. <div id="pkg-overview" class="toggleVisible">
  63. <div class="collapsed">
  64. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  65. </div>
  66. <div class="expanded">
  67. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  68. <p>
  69. Package http provides HTTP client and server implementations.
  70. </p>
  71. <p>
  72. Get, Head, Post, and PostForm make HTTP (or HTTPS) requests:
  73. </p>
  74. <pre>resp, err := http.Get(&#34;<a href="http://example.com/">http://example.com/</a>&#34;)
  75. ...
  76. resp, err := http.Post(&#34;<a href="http://example.com/upload">http://example.com/upload</a>&#34;, &#34;image/jpeg&#34;, &amp;buf)
  77. ...
  78. resp, err := http.PostForm(&#34;<a href="http://example.com/form">http://example.com/form</a>&#34;,
  79. url.Values{&#34;key&#34;: {&#34;Value&#34;}, &#34;id&#34;: {&#34;123&#34;}})
  80. </pre>
  81. <p>
  82. The client must close the response body when finished with it:
  83. </p>
  84. <pre>resp, err := http.Get(&#34;<a href="http://example.com/">http://example.com/</a>&#34;)
  85. if err != nil {
  86. // handle error
  87. }
  88. defer resp.Body.Close()
  89. body, err := ioutil.ReadAll(resp.Body)
  90. // ...
  91. </pre>
  92. <p>
  93. For control over HTTP client headers, redirect policy, and other
  94. settings, create a Client:
  95. </p>
  96. <pre>client := &amp;http.Client{
  97. CheckRedirect: redirectPolicyFunc,
  98. }
  99. resp, err := client.Get(&#34;<a href="http://example.com">http://example.com</a>&#34;)
  100. // ...
  101. req, err := http.NewRequest(&#34;GET&#34;, &#34;<a href="http://example.com">http://example.com</a>&#34;, nil)
  102. // ...
  103. req.Header.Add(&#34;If-None-Match&#34;, `W/&#34;wyzzy&#34;`)
  104. resp, err := client.Do(req)
  105. // ...
  106. </pre>
  107. <p>
  108. For control over proxies, TLS configuration, keep-alives,
  109. compression, and other settings, create a Transport:
  110. </p>
  111. <pre>tr := &amp;http.Transport{
  112. TLSClientConfig: &amp;tls.Config{RootCAs: pool},
  113. DisableCompression: true,
  114. }
  115. client := &amp;http.Client{Transport: tr}
  116. resp, err := client.Get(&#34;<a href="https://example.com">https://example.com</a>&#34;)
  117. </pre>
  118. <p>
  119. Clients and Transports are safe for concurrent use by multiple
  120. goroutines and for efficiency should only be created once and re-used.
  121. </p>
  122. <p>
  123. ListenAndServe starts an HTTP server with a given address and handler.
  124. The handler is usually nil, which means to use DefaultServeMux.
  125. Handle and HandleFunc add handlers to DefaultServeMux:
  126. </p>
  127. <pre>http.Handle(&#34;/foo&#34;, fooHandler)
  128. http.HandleFunc(&#34;/bar&#34;, func(w http.ResponseWriter, r *http.Request) {
  129. fmt.Fprintf(w, &#34;Hello, %q&#34;, html.EscapeString(r.URL.Path))
  130. })
  131. log.Fatal(http.ListenAndServe(&#34;:8080&#34;, nil))
  132. </pre>
  133. <p>
  134. More control over the server&#39;s behavior is available by creating a
  135. custom Server:
  136. </p>
  137. <pre>s := &amp;http.Server{
  138. Addr: &#34;:8080&#34;,
  139. Handler: myHandler,
  140. ReadTimeout: 10 * time.Second,
  141. WriteTimeout: 10 * time.Second,
  142. MaxHeaderBytes: 1 &lt;&lt; 20,
  143. }
  144. log.Fatal(s.ListenAndServe())
  145. </pre>
  146. <p>
  147. The http package has transparent support for the HTTP/2 protocol when
  148. using HTTPS. Programs that must disable HTTP/2 can do so by setting
  149. Transport.TLSNextProto (for clients) or Server.TLSNextProto (for
  150. servers) to a non-nil, empty map. Alternatively, the following GODEBUG
  151. environment variables are currently supported:
  152. </p>
  153. <pre>GODEBUG=http2client=0 # disable HTTP/2 client support
  154. GODEBUG=http2server=0 # disable HTTP/2 server support
  155. GODEBUG=http2debug=1 # enable verbose HTTP/2 debug logs
  156. GODEBUG=http2debug=2 # ... even more verbose, with frame dumps
  157. </pre>
  158. <p>
  159. The GODEBUG variables are not covered by Go&#39;s API compatibility promise.
  160. HTTP/2 support was added in Go 1.6. Please report any issues instead of
  161. disabling HTTP/2 support: <a href="https://golang.org/s/http2bug">https://golang.org/s/http2bug</a>
  162. </p>
  163. </div>
  164. </div>
  165. <div id="pkg-index" class="toggleVisible">
  166. <div class="collapsed">
  167. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  168. </div>
  169. <div class="expanded">
  170. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  171. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  172. <div id="manual-nav">
  173. <dl>
  174. <dd><a href="index.html#pkg-constants">Constants</a></dd>
  175. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  176. <dd><a href="index.html#CanonicalHeaderKey">func CanonicalHeaderKey(s string) string</a></dd>
  177. <dd><a href="index.html#DetectContentType">func DetectContentType(data []byte) string</a></dd>
  178. <dd><a href="index.html#Error">func Error(w ResponseWriter, error string, code int)</a></dd>
  179. <dd><a href="index.html#Handle">func Handle(pattern string, handler Handler)</a></dd>
  180. <dd><a href="index.html#HandleFunc">func HandleFunc(pattern string, handler func(ResponseWriter, *Request))</a></dd>
  181. <dd><a href="index.html#ListenAndServe">func ListenAndServe(addr string, handler Handler) error</a></dd>
  182. <dd><a href="index.html#ListenAndServeTLS">func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error</a></dd>
  183. <dd><a href="index.html#MaxBytesReader">func MaxBytesReader(w ResponseWriter, r io.ReadCloser, n int64) io.ReadCloser</a></dd>
  184. <dd><a href="index.html#NotFound">func NotFound(w ResponseWriter, r *Request)</a></dd>
  185. <dd><a href="index.html#ParseHTTPVersion">func ParseHTTPVersion(vers string) (major, minor int, ok bool)</a></dd>
  186. <dd><a href="index.html#ParseTime">func ParseTime(text string) (t time.Time, err error)</a></dd>
  187. <dd><a href="index.html#ProxyFromEnvironment">func ProxyFromEnvironment(req *Request) (*url.URL, error)</a></dd>
  188. <dd><a href="index.html#ProxyURL">func ProxyURL(fixedURL *url.URL) func(*Request) (*url.URL, error)</a></dd>
  189. <dd><a href="index.html#Redirect">func Redirect(w ResponseWriter, r *Request, urlStr string, code int)</a></dd>
  190. <dd><a href="index.html#Serve">func Serve(l net.Listener, handler Handler) error</a></dd>
  191. <dd><a href="index.html#ServeContent">func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker)</a></dd>
  192. <dd><a href="index.html#ServeFile">func ServeFile(w ResponseWriter, r *Request, name string)</a></dd>
  193. <dd><a href="index.html#SetCookie">func SetCookie(w ResponseWriter, cookie *Cookie)</a></dd>
  194. <dd><a href="index.html#StatusText">func StatusText(code int) string</a></dd>
  195. <dd><a href="index.html#Client">type Client</a></dd>
  196. <dd>&nbsp; &nbsp; <a href="index.html#Client.Do">func (c *Client) Do(req *Request) (resp *Response, err error)</a></dd>
  197. <dd>&nbsp; &nbsp; <a href="index.html#Client.Get">func (c *Client) Get(url string) (resp *Response, err error)</a></dd>
  198. <dd>&nbsp; &nbsp; <a href="index.html#Client.Head">func (c *Client) Head(url string) (resp *Response, err error)</a></dd>
  199. <dd>&nbsp; &nbsp; <a href="index.html#Client.Post">func (c *Client) Post(url string, bodyType string, body io.Reader) (resp *Response, err error)</a></dd>
  200. <dd>&nbsp; &nbsp; <a href="index.html#Client.PostForm">func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error)</a></dd>
  201. <dd><a href="index.html#CloseNotifier">type CloseNotifier</a></dd>
  202. <dd><a href="index.html#ConnState">type ConnState</a></dd>
  203. <dd>&nbsp; &nbsp; <a href="index.html#ConnState.String">func (c ConnState) String() string</a></dd>
  204. <dd><a href="index.html#Cookie">type Cookie</a></dd>
  205. <dd>&nbsp; &nbsp; <a href="index.html#Cookie.String">func (c *Cookie) String() string</a></dd>
  206. <dd><a href="index.html#CookieJar">type CookieJar</a></dd>
  207. <dd><a href="index.html#Dir">type Dir</a></dd>
  208. <dd>&nbsp; &nbsp; <a href="index.html#Dir.Open">func (d Dir) Open(name string) (File, error)</a></dd>
  209. <dd><a href="index.html#File">type File</a></dd>
  210. <dd><a href="index.html#FileSystem">type FileSystem</a></dd>
  211. <dd><a href="index.html#Flusher">type Flusher</a></dd>
  212. <dd><a href="index.html#Handler">type Handler</a></dd>
  213. <dd>&nbsp; &nbsp; <a href="index.html#FileServer">func FileServer(root FileSystem) Handler</a></dd>
  214. <dd>&nbsp; &nbsp; <a href="index.html#NotFoundHandler">func NotFoundHandler() Handler</a></dd>
  215. <dd>&nbsp; &nbsp; <a href="index.html#RedirectHandler">func RedirectHandler(url string, code int) Handler</a></dd>
  216. <dd>&nbsp; &nbsp; <a href="index.html#StripPrefix">func StripPrefix(prefix string, h Handler) Handler</a></dd>
  217. <dd>&nbsp; &nbsp; <a href="index.html#TimeoutHandler">func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler</a></dd>
  218. <dd><a href="index.html#HandlerFunc">type HandlerFunc</a></dd>
  219. <dd>&nbsp; &nbsp; <a href="index.html#HandlerFunc.ServeHTTP">func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request)</a></dd>
  220. <dd><a href="index.html#Header">type Header</a></dd>
  221. <dd>&nbsp; &nbsp; <a href="index.html#Header.Add">func (h Header) Add(key, value string)</a></dd>
  222. <dd>&nbsp; &nbsp; <a href="index.html#Header.Del">func (h Header) Del(key string)</a></dd>
  223. <dd>&nbsp; &nbsp; <a href="index.html#Header.Get">func (h Header) Get(key string) string</a></dd>
  224. <dd>&nbsp; &nbsp; <a href="index.html#Header.Set">func (h Header) Set(key, value string)</a></dd>
  225. <dd>&nbsp; &nbsp; <a href="index.html#Header.Write">func (h Header) Write(w io.Writer) error</a></dd>
  226. <dd>&nbsp; &nbsp; <a href="index.html#Header.WriteSubset">func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error</a></dd>
  227. <dd><a href="index.html#Hijacker">type Hijacker</a></dd>
  228. <dd><a href="index.html#ProtocolError">type ProtocolError</a></dd>
  229. <dd>&nbsp; &nbsp; <a href="index.html#ProtocolError.Error">func (err *ProtocolError) Error() string</a></dd>
  230. <dd><a href="index.html#Request">type Request</a></dd>
  231. <dd>&nbsp; &nbsp; <a href="index.html#NewRequest">func NewRequest(method, urlStr string, body io.Reader) (*Request, error)</a></dd>
  232. <dd>&nbsp; &nbsp; <a href="index.html#ReadRequest">func ReadRequest(b *bufio.Reader) (req *Request, err error)</a></dd>
  233. <dd>&nbsp; &nbsp; <a href="index.html#Request.AddCookie">func (r *Request) AddCookie(c *Cookie)</a></dd>
  234. <dd>&nbsp; &nbsp; <a href="index.html#Request.BasicAuth">func (r *Request) BasicAuth() (username, password string, ok bool)</a></dd>
  235. <dd>&nbsp; &nbsp; <a href="index.html#Request.Cookie">func (r *Request) Cookie(name string) (*Cookie, error)</a></dd>
  236. <dd>&nbsp; &nbsp; <a href="index.html#Request.Cookies">func (r *Request) Cookies() []*Cookie</a></dd>
  237. <dd>&nbsp; &nbsp; <a href="index.html#Request.FormFile">func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error)</a></dd>
  238. <dd>&nbsp; &nbsp; <a href="index.html#Request.FormValue">func (r *Request) FormValue(key string) string</a></dd>
  239. <dd>&nbsp; &nbsp; <a href="index.html#Request.MultipartReader">func (r *Request) MultipartReader() (*multipart.Reader, error)</a></dd>
  240. <dd>&nbsp; &nbsp; <a href="index.html#Request.ParseForm">func (r *Request) ParseForm() error</a></dd>
  241. <dd>&nbsp; &nbsp; <a href="index.html#Request.ParseMultipartForm">func (r *Request) ParseMultipartForm(maxMemory int64) error</a></dd>
  242. <dd>&nbsp; &nbsp; <a href="index.html#Request.PostFormValue">func (r *Request) PostFormValue(key string) string</a></dd>
  243. <dd>&nbsp; &nbsp; <a href="index.html#Request.ProtoAtLeast">func (r *Request) ProtoAtLeast(major, minor int) bool</a></dd>
  244. <dd>&nbsp; &nbsp; <a href="index.html#Request.Referer">func (r *Request) Referer() string</a></dd>
  245. <dd>&nbsp; &nbsp; <a href="index.html#Request.SetBasicAuth">func (r *Request) SetBasicAuth(username, password string)</a></dd>
  246. <dd>&nbsp; &nbsp; <a href="index.html#Request.UserAgent">func (r *Request) UserAgent() string</a></dd>
  247. <dd>&nbsp; &nbsp; <a href="index.html#Request.Write">func (r *Request) Write(w io.Writer) error</a></dd>
  248. <dd>&nbsp; &nbsp; <a href="index.html#Request.WriteProxy">func (r *Request) WriteProxy(w io.Writer) error</a></dd>
  249. <dd><a href="index.html#Response">type Response</a></dd>
  250. <dd>&nbsp; &nbsp; <a href="index.html#Get">func Get(url string) (resp *Response, err error)</a></dd>
  251. <dd>&nbsp; &nbsp; <a href="index.html#Head">func Head(url string) (resp *Response, err error)</a></dd>
  252. <dd>&nbsp; &nbsp; <a href="index.html#Post">func Post(url string, bodyType string, body io.Reader) (resp *Response, err error)</a></dd>
  253. <dd>&nbsp; &nbsp; <a href="index.html#PostForm">func PostForm(url string, data url.Values) (resp *Response, err error)</a></dd>
  254. <dd>&nbsp; &nbsp; <a href="index.html#ReadResponse">func ReadResponse(r *bufio.Reader, req *Request) (*Response, error)</a></dd>
  255. <dd>&nbsp; &nbsp; <a href="index.html#Response.Cookies">func (r *Response) Cookies() []*Cookie</a></dd>
  256. <dd>&nbsp; &nbsp; <a href="index.html#Response.Location">func (r *Response) Location() (*url.URL, error)</a></dd>
  257. <dd>&nbsp; &nbsp; <a href="index.html#Response.ProtoAtLeast">func (r *Response) ProtoAtLeast(major, minor int) bool</a></dd>
  258. <dd>&nbsp; &nbsp; <a href="index.html#Response.Write">func (r *Response) Write(w io.Writer) error</a></dd>
  259. <dd><a href="index.html#ResponseWriter">type ResponseWriter</a></dd>
  260. <dd><a href="index.html#RoundTripper">type RoundTripper</a></dd>
  261. <dd>&nbsp; &nbsp; <a href="index.html#NewFileTransport">func NewFileTransport(fs FileSystem) RoundTripper</a></dd>
  262. <dd><a href="index.html#ServeMux">type ServeMux</a></dd>
  263. <dd>&nbsp; &nbsp; <a href="index.html#NewServeMux">func NewServeMux() *ServeMux</a></dd>
  264. <dd>&nbsp; &nbsp; <a href="index.html#ServeMux.Handle">func (mux *ServeMux) Handle(pattern string, handler Handler)</a></dd>
  265. <dd>&nbsp; &nbsp; <a href="index.html#ServeMux.HandleFunc">func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request))</a></dd>
  266. <dd>&nbsp; &nbsp; <a href="index.html#ServeMux.Handler">func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string)</a></dd>
  267. <dd>&nbsp; &nbsp; <a href="index.html#ServeMux.ServeHTTP">func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request)</a></dd>
  268. <dd><a href="index.html#Server">type Server</a></dd>
  269. <dd>&nbsp; &nbsp; <a href="index.html#Server.ListenAndServe">func (srv *Server) ListenAndServe() error</a></dd>
  270. <dd>&nbsp; &nbsp; <a href="index.html#Server.ListenAndServeTLS">func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error</a></dd>
  271. <dd>&nbsp; &nbsp; <a href="index.html#Server.Serve">func (srv *Server) Serve(l net.Listener) error</a></dd>
  272. <dd>&nbsp; &nbsp; <a href="index.html#Server.SetKeepAlivesEnabled">func (srv *Server) SetKeepAlivesEnabled(v bool)</a></dd>
  273. <dd><a href="index.html#Transport">type Transport</a></dd>
  274. <dd>&nbsp; &nbsp; <a href="index.html#Transport.CancelRequest">func (t *Transport) CancelRequest(req *Request)</a></dd>
  275. <dd>&nbsp; &nbsp; <a href="index.html#Transport.CloseIdleConnections">func (t *Transport) CloseIdleConnections()</a></dd>
  276. <dd>&nbsp; &nbsp; <a href="index.html#Transport.RegisterProtocol">func (t *Transport) RegisterProtocol(scheme string, rt RoundTripper)</a></dd>
  277. <dd>&nbsp; &nbsp; <a href="index.html#Transport.RoundTrip">func (t *Transport) RoundTrip(req *Request) (*Response, error)</a></dd>
  278. </dl>
  279. </div><!-- #manual-nav -->
  280. <div id="pkg-examples">
  281. <h4>Examples</h4>
  282. <dl>
  283. <dd><a class="exampleLink" href="index.html#example_FileServer">FileServer</a></dd>
  284. <dd><a class="exampleLink" href="index.html#example_FileServer_stripPrefix">FileServer (StripPrefix)</a></dd>
  285. <dd><a class="exampleLink" href="index.html#example_Get">Get</a></dd>
  286. <dd><a class="exampleLink" href="index.html#example_Hijacker">Hijacker</a></dd>
  287. <dd><a class="exampleLink" href="index.html#example_ResponseWriter_trailers">ResponseWriter (Trailers)</a></dd>
  288. <dd><a class="exampleLink" href="index.html#example_ServeMux_Handle">ServeMux.Handle</a></dd>
  289. <dd><a class="exampleLink" href="index.html#example_StripPrefix">StripPrefix</a></dd>
  290. </dl>
  291. </div>
  292. <h4>Package files</h4>
  293. <p>
  294. <span style="font-size:90%">
  295. <a href="http://localhost:6060/src/net/http/client.go">client.go</a>
  296. <a href="http://localhost:6060/src/net/http/cookie.go">cookie.go</a>
  297. <a href="http://localhost:6060/src/net/http/doc.go">doc.go</a>
  298. <a href="http://localhost:6060/src/net/http/filetransport.go">filetransport.go</a>
  299. <a href="http://localhost:6060/src/net/http/fs.go">fs.go</a>
  300. <a href="http://localhost:6060/src/net/http/h2_bundle.go">h2_bundle.go</a>
  301. <a href="http://localhost:6060/src/net/http/header.go">header.go</a>
  302. <a href="http://localhost:6060/src/net/http/jar.go">jar.go</a>
  303. <a href="http://localhost:6060/src/net/http/lex.go">lex.go</a>
  304. <a href="http://localhost:6060/src/net/http/method.go">method.go</a>
  305. <a href="http://localhost:6060/src/net/http/request.go">request.go</a>
  306. <a href="http://localhost:6060/src/net/http/response.go">response.go</a>
  307. <a href="http://localhost:6060/src/net/http/server.go">server.go</a>
  308. <a href="http://localhost:6060/src/net/http/sniff.go">sniff.go</a>
  309. <a href="http://localhost:6060/src/net/http/status.go">status.go</a>
  310. <a href="http://localhost:6060/src/net/http/transfer.go">transfer.go</a>
  311. <a href="http://localhost:6060/src/net/http/transport.go">transport.go</a>
  312. </span>
  313. </p>
  314. </div><!-- .expanded -->
  315. </div><!-- #pkg-index -->
  316. <div id="pkg-callgraph" class="toggle" style="display: none">
  317. <div class="collapsed">
  318. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  319. </div> <!-- .expanded -->
  320. <div class="expanded">
  321. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  322. <p>
  323. In the call graph viewer below, each node
  324. is a function belonging to this package
  325. and its children are the functions it
  326. calls&mdash;perhaps dynamically.
  327. </p>
  328. <p>
  329. The root nodes are the entry points of the
  330. package: functions that may be called from
  331. outside the package.
  332. There may be non-exported or anonymous
  333. functions among them if they are called
  334. dynamically from another package.
  335. </p>
  336. <p>
  337. Click a node to visit that function's source code.
  338. From there you can visit its callers by
  339. clicking its declaring <code>func</code>
  340. token.
  341. </p>
  342. <p>
  343. Functions may be omitted if they were
  344. determined to be unreachable in the
  345. particular programs or tests that were
  346. analyzed.
  347. </p>
  348. <!-- Zero means show all package entry points. -->
  349. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  350. </div>
  351. </div> <!-- #pkg-callgraph -->
  352. <h2 id="pkg-constants">Constants</h2>
  353. <pre>const (
  354. <span id="MethodGet">MethodGet</span> = &#34;GET&#34;
  355. <span id="MethodHead">MethodHead</span> = &#34;HEAD&#34;
  356. <span id="MethodPost">MethodPost</span> = &#34;POST&#34;
  357. <span id="MethodPut">MethodPut</span> = &#34;PUT&#34;
  358. <span id="MethodPatch">MethodPatch</span> = &#34;PATCH&#34; <span class="comment">// RFC 5741</span>
  359. <span id="MethodDelete">MethodDelete</span> = &#34;DELETE&#34;
  360. <span id="MethodConnect">MethodConnect</span> = &#34;CONNECT&#34;
  361. <span id="MethodOptions">MethodOptions</span> = &#34;OPTIONS&#34;
  362. <span id="MethodTrace">MethodTrace</span> = &#34;TRACE&#34;
  363. )</pre>
  364. <p>
  365. Common HTTP methods.
  366. </p>
  367. <p>
  368. Unless otherwise noted, these are defined in RFC 7231 section 4.3.
  369. </p>
  370. <pre>const (
  371. <span id="StatusContinue">StatusContinue</span> = 100
  372. <span id="StatusSwitchingProtocols">StatusSwitchingProtocols</span> = 101
  373. <span id="StatusOK">StatusOK</span> = 200
  374. <span id="StatusCreated">StatusCreated</span> = 201
  375. <span id="StatusAccepted">StatusAccepted</span> = 202
  376. <span id="StatusNonAuthoritativeInfo">StatusNonAuthoritativeInfo</span> = 203
  377. <span id="StatusNoContent">StatusNoContent</span> = 204
  378. <span id="StatusResetContent">StatusResetContent</span> = 205
  379. <span id="StatusPartialContent">StatusPartialContent</span> = 206
  380. <span id="StatusMultipleChoices">StatusMultipleChoices</span> = 300
  381. <span id="StatusMovedPermanently">StatusMovedPermanently</span> = 301
  382. <span id="StatusFound">StatusFound</span> = 302
  383. <span id="StatusSeeOther">StatusSeeOther</span> = 303
  384. <span id="StatusNotModified">StatusNotModified</span> = 304
  385. <span id="StatusUseProxy">StatusUseProxy</span> = 305
  386. <span id="StatusTemporaryRedirect">StatusTemporaryRedirect</span> = 307
  387. <span id="StatusBadRequest">StatusBadRequest</span> = 400
  388. <span id="StatusUnauthorized">StatusUnauthorized</span> = 401
  389. <span id="StatusPaymentRequired">StatusPaymentRequired</span> = 402
  390. <span id="StatusForbidden">StatusForbidden</span> = 403
  391. <span id="StatusNotFound">StatusNotFound</span> = 404
  392. <span id="StatusMethodNotAllowed">StatusMethodNotAllowed</span> = 405
  393. <span id="StatusNotAcceptable">StatusNotAcceptable</span> = 406
  394. <span id="StatusProxyAuthRequired">StatusProxyAuthRequired</span> = 407
  395. <span id="StatusRequestTimeout">StatusRequestTimeout</span> = 408
  396. <span id="StatusConflict">StatusConflict</span> = 409
  397. <span id="StatusGone">StatusGone</span> = 410
  398. <span id="StatusLengthRequired">StatusLengthRequired</span> = 411
  399. <span id="StatusPreconditionFailed">StatusPreconditionFailed</span> = 412
  400. <span id="StatusRequestEntityTooLarge">StatusRequestEntityTooLarge</span> = 413
  401. <span id="StatusRequestURITooLong">StatusRequestURITooLong</span> = 414
  402. <span id="StatusUnsupportedMediaType">StatusUnsupportedMediaType</span> = 415
  403. <span id="StatusRequestedRangeNotSatisfiable">StatusRequestedRangeNotSatisfiable</span> = 416
  404. <span id="StatusExpectationFailed">StatusExpectationFailed</span> = 417
  405. <span id="StatusTeapot">StatusTeapot</span> = 418
  406. <span id="StatusPreconditionRequired">StatusPreconditionRequired</span> = 428
  407. <span id="StatusTooManyRequests">StatusTooManyRequests</span> = 429
  408. <span id="StatusRequestHeaderFieldsTooLarge">StatusRequestHeaderFieldsTooLarge</span> = 431
  409. <span id="StatusUnavailableForLegalReasons">StatusUnavailableForLegalReasons</span> = 451
  410. <span id="StatusInternalServerError">StatusInternalServerError</span> = 500
  411. <span id="StatusNotImplemented">StatusNotImplemented</span> = 501
  412. <span id="StatusBadGateway">StatusBadGateway</span> = 502
  413. <span id="StatusServiceUnavailable">StatusServiceUnavailable</span> = 503
  414. <span id="StatusGatewayTimeout">StatusGatewayTimeout</span> = 504
  415. <span id="StatusHTTPVersionNotSupported">StatusHTTPVersionNotSupported</span> = 505
  416. <span id="StatusNetworkAuthenticationRequired">StatusNetworkAuthenticationRequired</span> = 511
  417. )</pre>
  418. <p>
  419. HTTP status codes, defined in RFC 2616.
  420. </p>
  421. <pre>const <span id="DefaultMaxHeaderBytes">DefaultMaxHeaderBytes</span> = 1 &lt;&lt; 20 <span class="comment">// 1 MB</span>
  422. </pre>
  423. <p>
  424. DefaultMaxHeaderBytes is the maximum permitted size of the headers
  425. in an HTTP request.
  426. This can be overridden by setting Server.MaxHeaderBytes.
  427. </p>
  428. <pre>const <span id="DefaultMaxIdleConnsPerHost">DefaultMaxIdleConnsPerHost</span> = 2</pre>
  429. <p>
  430. DefaultMaxIdleConnsPerHost is the default value of Transport&#39;s
  431. MaxIdleConnsPerHost.
  432. </p>
  433. <pre>const <span id="TimeFormat">TimeFormat</span> = &#34;Mon, 02 Jan 2006 15:04:05 GMT&#34;</pre>
  434. <p>
  435. TimeFormat is the time format to use when generating times in HTTP
  436. headers. It is like time.RFC1123 but hard-codes GMT as the time
  437. zone. The time being formatted must be in UTC for Format to
  438. generate the correct format.
  439. </p>
  440. <p>
  441. For parsing this time format, see ParseTime.
  442. </p>
  443. <h2 id="pkg-variables">Variables</h2>
  444. <pre>var (
  445. <span id="ErrHeaderTooLong">ErrHeaderTooLong</span> = &amp;<a href="index.html#ProtocolError">ProtocolError</a>{&#34;header too long&#34;}
  446. <span id="ErrShortBody">ErrShortBody</span> = &amp;<a href="index.html#ProtocolError">ProtocolError</a>{&#34;entity body too short&#34;}
  447. <span id="ErrNotSupported">ErrNotSupported</span> = &amp;<a href="index.html#ProtocolError">ProtocolError</a>{&#34;feature not supported&#34;}
  448. <span id="ErrUnexpectedTrailer">ErrUnexpectedTrailer</span> = &amp;<a href="index.html#ProtocolError">ProtocolError</a>{&#34;trailer header without chunked transfer encoding&#34;}
  449. <span id="ErrMissingContentLength">ErrMissingContentLength</span> = &amp;<a href="index.html#ProtocolError">ProtocolError</a>{&#34;missing ContentLength in HEAD response&#34;}
  450. <span id="ErrNotMultipart">ErrNotMultipart</span> = &amp;<a href="index.html#ProtocolError">ProtocolError</a>{&#34;request Content-Type isn&#39;t multipart/form-data&#34;}
  451. <span id="ErrMissingBoundary">ErrMissingBoundary</span> = &amp;<a href="index.html#ProtocolError">ProtocolError</a>{&#34;no multipart boundary param in Content-Type&#34;}
  452. )</pre>
  453. <pre>var (
  454. <span id="ErrWriteAfterFlush">ErrWriteAfterFlush</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;Conn.Write called after Flush&#34;)
  455. <span id="ErrBodyNotAllowed">ErrBodyNotAllowed</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;http: request method or response status code does not allow body&#34;)
  456. <span id="ErrHijacked">ErrHijacked</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;Conn has been hijacked&#34;)
  457. <span id="ErrContentLength">ErrContentLength</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;Conn.Write wrote more than the declared Content-Length&#34;)
  458. )</pre>
  459. <p>
  460. Errors introduced by the HTTP server.
  461. </p>
  462. <pre>var <span id="DefaultClient">DefaultClient</span> = &amp;<a href="index.html#Client">Client</a>{}</pre>
  463. <p>
  464. DefaultClient is the default Client and is used by Get, Head, and Post.
  465. </p>
  466. <pre>var <span id="DefaultServeMux">DefaultServeMux</span> = <a href="index.html#NewServeMux">NewServeMux</a>()</pre>
  467. <p>
  468. DefaultServeMux is the default ServeMux used by Serve.
  469. </p>
  470. <pre>var <span id="ErrBodyReadAfterClose">ErrBodyReadAfterClose</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;http: invalid Read on closed Body&#34;)</pre>
  471. <p>
  472. ErrBodyReadAfterClose is returned when reading a Request or Response
  473. Body after the body has been closed. This typically happens when the body is
  474. read after an HTTP Handler calls WriteHeader or Write on its
  475. ResponseWriter.
  476. </p>
  477. <pre>var <span id="ErrHandlerTimeout">ErrHandlerTimeout</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;http: Handler timeout&#34;)</pre>
  478. <p>
  479. ErrHandlerTimeout is returned on ResponseWriter Write calls
  480. in handlers which have timed out.
  481. </p>
  482. <pre>var <span id="ErrLineTooLong">ErrLineTooLong</span> = <a href="internal/index.html">internal</a>.<a href="internal/index.html#ErrLineTooLong">ErrLineTooLong</a></pre>
  483. <p>
  484. ErrLineTooLong is returned when reading request or response bodies
  485. with malformed chunked encoding.
  486. </p>
  487. <pre>var <span id="ErrMissingFile">ErrMissingFile</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;http: no such file&#34;)</pre>
  488. <p>
  489. ErrMissingFile is returned by FormFile when the provided file field name
  490. is either not present in the request or not a file field.
  491. </p>
  492. <pre>var <span id="ErrNoCookie">ErrNoCookie</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;http: named cookie not present&#34;)</pre>
  493. <p>
  494. ErrNoCookie is returned by Request&#39;s Cookie method when a cookie is not found.
  495. </p>
  496. <pre>var <span id="ErrNoLocation">ErrNoLocation</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;http: no Location header in response&#34;)</pre>
  497. <p>
  498. ErrNoLocation is returned by Response&#39;s Location method
  499. when no Location header is present.
  500. </p>
  501. <pre>var <span id="ErrSkipAltProtocol">ErrSkipAltProtocol</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;net/http: skip alternate protocol&#34;)</pre>
  502. <p>
  503. ErrSkipAltProtocol is a sentinel error value defined by Transport.RegisterProtocol.
  504. </p>
  505. <h2 id="CanonicalHeaderKey">func <a href="http://localhost:6060/src/net/http/header.go?s=4562:4602#L163">CanonicalHeaderKey</a></h2>
  506. <pre>func CanonicalHeaderKey(s <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#string">string</a></pre>
  507. <p>
  508. CanonicalHeaderKey returns the canonical format of the
  509. header key s. The canonicalization converts the first
  510. letter and any letter following a hyphen to upper case;
  511. the rest are converted to lowercase. For example, the
  512. canonical key for &#34;accept-encoding&#34; is &#34;Accept-Encoding&#34;.
  513. If s contains a space or invalid header field bytes, it is
  514. returned without modifications.
  515. </p>
  516. <h2 id="DetectContentType">func <a href="http://localhost:6060/src/net/http/sniff.go?s=648:690#L11">DetectContentType</a></h2>
  517. <pre>func DetectContentType(data []<a href="../../builtin/index.html#byte">byte</a>) <a href="../../builtin/index.html#string">string</a></pre>
  518. <p>
  519. DetectContentType implements the algorithm described
  520. at <a href="http://mimesniff.spec.whatwg.org/">http://mimesniff.spec.whatwg.org/</a> to determine the
  521. Content-Type of the given data. It considers at most the
  522. first 512 bytes of data. DetectContentType always returns
  523. a valid MIME type: if it cannot determine a more specific one, it
  524. returns &#34;application/octet-stream&#34;.
  525. </p>
  526. <h2 id="Error">func <a href="http://localhost:6060/src/net/http/server.go?s=47192:47244#L1615">Error</a></h2>
  527. <pre>func Error(w <a href="index.html#ResponseWriter">ResponseWriter</a>, error <a href="../../builtin/index.html#string">string</a>, code <a href="../../builtin/index.html#int">int</a>)</pre>
  528. <p>
  529. Error replies to the request with the specified error message and HTTP code.
  530. The error message should be plain text.
  531. </p>
  532. <h2 id="Handle">func <a href="http://localhost:6060/src/net/http/server.go?s=57817:57861#L1951">Handle</a></h2>
  533. <pre>func Handle(pattern <a href="../../builtin/index.html#string">string</a>, handler <a href="index.html#Handler">Handler</a>)</pre>
  534. <p>
  535. Handle registers the handler for the given pattern
  536. in the DefaultServeMux.
  537. The documentation for ServeMux explains how patterns are matched.
  538. </p>
  539. <h2 id="HandleFunc">func <a href="http://localhost:6060/src/net/http/server.go?s=58071:58142#L1956">HandleFunc</a></h2>
  540. <pre>func HandleFunc(pattern <a href="../../builtin/index.html#string">string</a>, handler func(<a href="index.html#ResponseWriter">ResponseWriter</a>, *<a href="index.html#Request">Request</a>))</pre>
  541. <p>
  542. HandleFunc registers the handler function for the given pattern
  543. in the DefaultServeMux.
  544. The documentation for ServeMux explains how patterns are matched.
  545. </p>
  546. <h2 id="ListenAndServe">func <a href="http://localhost:6060/src/net/http/server.go?s=65528:65583#L2183">ListenAndServe</a></h2>
  547. <pre>func ListenAndServe(addr <a href="../../builtin/index.html#string">string</a>, handler <a href="index.html#Handler">Handler</a>) <a href="../../builtin/index.html#error">error</a></pre>
  548. <p>
  549. ListenAndServe listens on the TCP network address addr
  550. and then calls Serve with handler to handle requests
  551. on incoming connections.
  552. Accepted connections are configured to enable TCP keep-alives.
  553. Handler is typically nil, in which case the DefaultServeMux is
  554. used.
  555. </p>
  556. <p>
  557. A trivial example server is:
  558. </p>
  559. <pre>package main
  560. import (
  561. &#34;io&#34;
  562. &#34;net/http&#34;
  563. &#34;log&#34;
  564. )
  565. // hello world, the web server
  566. func HelloServer(w http.ResponseWriter, req *http.Request) {
  567. io.WriteString(w, &#34;hello, world!\n&#34;)
  568. }
  569. func main() {
  570. http.HandleFunc(&#34;/hello&#34;, HelloServer)
  571. log.Fatal(http.ListenAndServe(&#34;:12345&#34;, nil))
  572. }
  573. </pre>
  574. <p>
  575. ListenAndServe always returns a non-nil error.
  576. </p>
  577. <h2 id="ListenAndServeTLS">func <a href="http://localhost:6060/src/net/http/server.go?s=66669:66746#L2216">ListenAndServeTLS</a></h2>
  578. <pre>func ListenAndServeTLS(addr, certFile, keyFile <a href="../../builtin/index.html#string">string</a>, handler <a href="index.html#Handler">Handler</a>) <a href="../../builtin/index.html#error">error</a></pre>
  579. <p>
  580. ListenAndServeTLS acts identically to ListenAndServe, except that it
  581. expects HTTPS connections. Additionally, files containing a certificate and
  582. matching private key for the server must be provided. If the certificate
  583. is signed by a certificate authority, the certFile should be the concatenation
  584. of the server&#39;s certificate, any intermediates, and the CA&#39;s certificate.
  585. </p>
  586. <p>
  587. A trivial example server is:
  588. </p>
  589. <pre>import (
  590. &#34;log&#34;
  591. &#34;net/http&#34;
  592. )
  593. func handler(w http.ResponseWriter, req *http.Request) {
  594. w.Header().Set(&#34;Content-Type&#34;, &#34;text/plain&#34;)
  595. w.Write([]byte(&#34;This is an example server.\n&#34;))
  596. }
  597. func main() {
  598. http.HandleFunc(&#34;/&#34;, handler)
  599. log.Printf(&#34;About to listen on 10443. Go to <a href="https://127.0.0.1:10443/">https://127.0.0.1:10443/</a>&#34;)
  600. err := http.ListenAndServeTLS(&#34;:10443&#34;, &#34;cert.pem&#34;, &#34;key.pem&#34;, nil)
  601. log.Fatal(err)
  602. }
  603. </pre>
  604. <p>
  605. One can use generate_cert.go in crypto/tls to generate cert.pem and key.pem.
  606. </p>
  607. <p>
  608. ListenAndServeTLS always returns a non-nil error.
  609. </p>
  610. <h2 id="MaxBytesReader">func <a href="http://localhost:6060/src/net/http/request.go?s=25449:25526#L796">MaxBytesReader</a></h2>
  611. <pre>func MaxBytesReader(w <a href="index.html#ResponseWriter">ResponseWriter</a>, r <a href="../../io/index.html">io</a>.<a href="../../io/index.html#ReadCloser">ReadCloser</a>, n <a href="../../builtin/index.html#int64">int64</a>) <a href="../../io/index.html">io</a>.<a href="../../io/index.html#ReadCloser">ReadCloser</a></pre>
  612. <p>
  613. MaxBytesReader is similar to io.LimitReader but is intended for
  614. limiting the size of incoming request bodies. In contrast to
  615. io.LimitReader, MaxBytesReader&#39;s result is a ReadCloser, returns a
  616. non-EOF error for a Read beyond the limit, and closes the
  617. underlying reader when its Close method is called.
  618. </p>
  619. <p>
  620. MaxBytesReader prevents clients from accidentally or maliciously
  621. sending a large request and wasting server resources.
  622. </p>
  623. <h2 id="NotFound">func <a href="http://localhost:6060/src/net/http/server.go?s=47478:47521#L1623">NotFound</a></h2>
  624. <pre>func NotFound(w <a href="index.html#ResponseWriter">ResponseWriter</a>, r *<a href="index.html#Request">Request</a>)</pre>
  625. <p>
  626. NotFound replies to the request with an HTTP 404 not found error.
  627. </p>
  628. <h2 id="ParseHTTPVersion">func <a href="http://localhost:6060/src/net/http/request.go?s=17704:17766#L533">ParseHTTPVersion</a></h2>
  629. <pre>func ParseHTTPVersion(vers <a href="../../builtin/index.html#string">string</a>) (major, minor <a href="../../builtin/index.html#int">int</a>, ok <a href="../../builtin/index.html#bool">bool</a>)</pre>
  630. <p>
  631. ParseHTTPVersion parses a HTTP version string.
  632. &#34;HTTP/1.0&#34; returns (1, 0, true).
  633. </p>
  634. <h2 id="ParseTime">func <a href="http://localhost:6060/src/net/http/header.go?s=1908:1960#L69">ParseTime</a></h2>
  635. <pre>func ParseTime(text <a href="../../builtin/index.html#string">string</a>) (t <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Time">Time</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  636. <p>
  637. ParseTime parses a time header (such as the Date: header),
  638. trying each of the three formats allowed by HTTP/1.1:
  639. TimeFormat, time.RFC850, and time.ANSIC.
  640. </p>
  641. <h2 id="ProxyFromEnvironment">func <a href="http://localhost:6060/src/net/http/transport.go?s=7718:7775#L198">ProxyFromEnvironment</a></h2>
  642. <pre>func ProxyFromEnvironment(req *<a href="index.html#Request">Request</a>) (*<a href="../url/index.html">url</a>.<a href="../url/index.html#URL">URL</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  643. <p>
  644. ProxyFromEnvironment returns the URL of the proxy to use for a
  645. given request, as indicated by the environment variables
  646. HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions
  647. thereof). HTTPS_PROXY takes precedence over HTTP_PROXY for https
  648. requests.
  649. </p>
  650. <p>
  651. The environment values may be either a complete URL or a
  652. &#34;host[:port]&#34;, in which case the &#34;http&#34; scheme is assumed.
  653. An error is returned if the value is a different form.
  654. </p>
  655. <p>
  656. A nil URL and nil error are returned if no proxy is defined in the
  657. environment, or a proxy should not be used for the given request,
  658. as defined by NO_PROXY.
  659. </p>
  660. <p>
  661. As a special case, if req.URL.Host is &#34;localhost&#34; (with or without
  662. a port number), then a nil URL and nil error will be returned.
  663. </p>
  664. <h2 id="ProxyURL">func <a href="http://localhost:6060/src/net/http/transport.go?s=8577:8642#L229">ProxyURL</a></h2>
  665. <pre>func ProxyURL(fixedURL *<a href="../url/index.html">url</a>.<a href="../url/index.html#URL">URL</a>) func(*<a href="index.html#Request">Request</a>) (*<a href="../url/index.html">url</a>.<a href="../url/index.html#URL">URL</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  666. <p>
  667. ProxyURL returns a proxy function (for use in a Transport)
  668. that always returns the same URL.
  669. </p>
  670. <h2 id="Redirect">func <a href="http://localhost:6060/src/net/http/server.go?s=48569:48637#L1653">Redirect</a></h2>
  671. <pre>func Redirect(w <a href="index.html#ResponseWriter">ResponseWriter</a>, r *<a href="index.html#Request">Request</a>, urlStr <a href="../../builtin/index.html#string">string</a>, code <a href="../../builtin/index.html#int">int</a>)</pre>
  672. <p>
  673. Redirect replies to the request with a redirect to url,
  674. which may be a path relative to the request path.
  675. </p>
  676. <p>
  677. The provided code should be in the 3xx range and is usually
  678. StatusMovedPermanently, StatusFound or StatusSeeOther.
  679. </p>
  680. <h2 id="Serve">func <a href="http://localhost:6060/src/net/http/server.go?s=58455:58504#L1964">Serve</a></h2>
  681. <pre>func Serve(l <a href="../index.html">net</a>.<a href="../index.html#Listener">Listener</a>, handler <a href="index.html#Handler">Handler</a>) <a href="../../builtin/index.html#error">error</a></pre>
  682. <p>
  683. Serve accepts incoming HTTP connections on the listener l,
  684. creating a new service goroutine for each. The service goroutines
  685. read requests and then call handler to reply to them.
  686. Handler is typically nil, in which case the DefaultServeMux is used.
  687. </p>
  688. <h2 id="ServeContent">func <a href="http://localhost:6060/src/net/http/fs.go?s=3711:3815#L112">ServeContent</a></h2>
  689. <pre>func ServeContent(w <a href="index.html#ResponseWriter">ResponseWriter</a>, req *<a href="index.html#Request">Request</a>, name <a href="../../builtin/index.html#string">string</a>, modtime <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Time">Time</a>, content <a href="../../io/index.html">io</a>.<a href="../../io/index.html#ReadSeeker">ReadSeeker</a>)</pre>
  690. <p>
  691. ServeContent replies to the request using the content in the
  692. provided ReadSeeker. The main benefit of ServeContent over io.Copy
  693. is that it handles Range requests properly, sets the MIME type, and
  694. handles If-Modified-Since requests.
  695. </p>
  696. <p>
  697. If the response&#39;s Content-Type header is not set, ServeContent
  698. first tries to deduce the type from name&#39;s file extension and,
  699. if that fails, falls back to reading the first block of the content
  700. and passing it to DetectContentType.
  701. The name is otherwise unused; in particular it can be empty and is
  702. never sent in the response.
  703. </p>
  704. <p>
  705. If modtime is not the zero time or Unix epoch, ServeContent
  706. includes it in a Last-Modified header in the response. If the
  707. request includes an If-Modified-Since header, ServeContent uses
  708. modtime to decide whether the content needs to be sent at all.
  709. </p>
  710. <p>
  711. The content&#39;s Seek method must work: ServeContent uses
  712. a seek to the end of the content to determine its size.
  713. </p>
  714. <p>
  715. If the caller has set w&#39;s ETag header, ServeContent uses it to
  716. handle requests using If-Range and If-None-Match.
  717. </p>
  718. <p>
  719. Note that *os.File implements the io.ReadSeeker interface.
  720. </p>
  721. <h2 id="ServeFile">func <a href="http://localhost:6060/src/net/http/fs.go?s=14478:14535#L454">ServeFile</a></h2>
  722. <pre>func ServeFile(w <a href="index.html#ResponseWriter">ResponseWriter</a>, r *<a href="index.html#Request">Request</a>, name <a href="../../builtin/index.html#string">string</a>)</pre>
  723. <p>
  724. ServeFile replies to the request with the contents of the named
  725. file or directory.
  726. </p>
  727. <p>
  728. If the provided file or direcory name is a relative path, it is
  729. interpreted relative to the current directory and may ascend to parent
  730. directories. If the provided name is constructed from user input, it
  731. should be sanitized before calling ServeFile. As a precaution, ServeFile
  732. will reject requests where r.URL.Path contains a &#34;..&#34; path element.
  733. </p>
  734. <p>
  735. As a special case, ServeFile redirects any request where r.URL.Path
  736. ends in &#34;/index.html&#34; to the same path, without the final
  737. &#34;index.html&#34;. To avoid such redirects either modify the path or
  738. use ServeContent.
  739. </p>
  740. <h2 id="SetCookie">func <a href="http://localhost:6060/src/net/http/cookie.go?s=3059:3107#L120">SetCookie</a></h2>
  741. <pre>func SetCookie(w <a href="index.html#ResponseWriter">ResponseWriter</a>, cookie *<a href="index.html#Cookie">Cookie</a>)</pre>
  742. <p>
  743. SetCookie adds a Set-Cookie header to the provided ResponseWriter&#39;s headers.
  744. The provided cookie must have a valid Name. Invalid cookies may be
  745. silently dropped.
  746. </p>
  747. <h2 id="StatusText">func <a href="http://localhost:6060/src/net/http/status.go?s=4647:4679#L106">StatusText</a></h2>
  748. <pre>func StatusText(code <a href="../../builtin/index.html#int">int</a>) <a href="../../builtin/index.html#string">string</a></pre>
  749. <p>
  750. StatusText returns a text for the HTTP status code. It returns the empty
  751. string if the code is unknown.
  752. </p>
  753. <h2 id="Client">type <a href="http://localhost:6060/src/net/http/client.go?s=896:2598#L26">Client</a></h2>
  754. <pre>type Client struct {
  755. <span class="comment">// Transport specifies the mechanism by which individual</span>
  756. <span class="comment">// HTTP requests are made.</span>
  757. <span class="comment">// If nil, DefaultTransport is used.</span>
  758. Transport <a href="index.html#RoundTripper">RoundTripper</a>
  759. <span class="comment">// CheckRedirect specifies the policy for handling redirects.</span>
  760. <span class="comment">// If CheckRedirect is not nil, the client calls it before</span>
  761. <span class="comment">// following an HTTP redirect. The arguments req and via are</span>
  762. <span class="comment">// the upcoming request and the requests made already, oldest</span>
  763. <span class="comment">// first. If CheckRedirect returns an error, the Client&#39;s Get</span>
  764. <span class="comment">// method returns both the previous Response and</span>
  765. <span class="comment">// CheckRedirect&#39;s error (wrapped in a url.Error) instead of</span>
  766. <span class="comment">// issuing the Request req.</span>
  767. <span class="comment">//</span>
  768. <span class="comment">// If CheckRedirect is nil, the Client uses its default policy,</span>
  769. <span class="comment">// which is to stop after 10 consecutive requests.</span>
  770. CheckRedirect func(req *<a href="index.html#Request">Request</a>, via []*<a href="index.html#Request">Request</a>) <a href="../../builtin/index.html#error">error</a>
  771. <span class="comment">// Jar specifies the cookie jar.</span>
  772. <span class="comment">// If Jar is nil, cookies are not sent in requests and ignored</span>
  773. <span class="comment">// in responses.</span>
  774. Jar <a href="index.html#CookieJar">CookieJar</a>
  775. <span class="comment">// Timeout specifies a time limit for requests made by this</span>
  776. <span class="comment">// Client. The timeout includes connection time, any</span>
  777. <span class="comment">// redirects, and reading the response body. The timer remains</span>
  778. <span class="comment">// running after Get, Head, Post, or Do return and will</span>
  779. <span class="comment">// interrupt reading of the Response.Body.</span>
  780. <span class="comment">//</span>
  781. <span class="comment">// A Timeout of zero means no timeout.</span>
  782. <span class="comment">//</span>
  783. <span class="comment">// The Client cancels requests to the underlying Transport</span>
  784. <span class="comment">// using the Request.Cancel mechanism. Requests passed</span>
  785. <span class="comment">// to Client.Do may still set Request.Cancel; both will</span>
  786. <span class="comment">// cancel the request.</span>
  787. <span class="comment">//</span>
  788. <span class="comment">// For compatibility, the Client will also use the deprecated</span>
  789. <span class="comment">// CancelRequest method on Transport if found. New</span>
  790. <span class="comment">// RoundTripper implementations should use Request.Cancel</span>
  791. <span class="comment">// instead of implementing CancelRequest.</span>
  792. Timeout <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Duration">Duration</a>
  793. }</pre>
  794. <p>
  795. A Client is an HTTP client. Its zero value (DefaultClient) is a
  796. usable client that uses DefaultTransport.
  797. </p>
  798. <p>
  799. The Client&#39;s Transport typically has internal state (cached TCP
  800. connections), so Clients should be reused instead of created as
  801. needed. Clients are safe for concurrent use by multiple goroutines.
  802. </p>
  803. <p>
  804. A Client is higher-level than a RoundTripper (such as Transport)
  805. and additionally handles HTTP details such as cookies and
  806. redirects.
  807. </p>
  808. <h3 id="Client.Do">func (*Client) <a href="http://localhost:6060/src/net/http/client.go?s=6511:6572#L175">Do</a></h3>
  809. <pre>func (c *<a href="index.html#Client">Client</a>) Do(req *<a href="index.html#Request">Request</a>) (resp *<a href="index.html#Response">Response</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  810. <p>
  811. Do sends an HTTP request and returns an HTTP response, following
  812. policy (e.g. redirects, cookies, auth) as configured on the client.
  813. </p>
  814. <p>
  815. An error is returned if caused by client policy (such as
  816. CheckRedirect), or if there was an HTTP protocol error.
  817. A non-2xx response doesn&#39;t cause an error.
  818. </p>
  819. <p>
  820. When err is nil, resp always contains a non-nil resp.Body.
  821. </p>
  822. <p>
  823. Callers should close resp.Body when done reading from it. If
  824. resp.Body is not closed, the Client&#39;s underlying RoundTripper
  825. (typically Transport) may not be able to re-use a persistent TCP
  826. connection to the server for a subsequent &#34;keep-alive&#34; request.
  827. </p>
  828. <p>
  829. The request Body, if non-nil, will be closed by the underlying
  830. Transport, even on errors.
  831. </p>
  832. <p>
  833. Generally Get, Post, or PostForm will be used instead of Do.
  834. </p>
  835. <h3 id="Client.Get">func (*Client) <a href="http://localhost:6060/src/net/http/client.go?s=12938:12998#L407">Get</a></h3>
  836. <pre>func (c *<a href="index.html#Client">Client</a>) Get(url <a href="../../builtin/index.html#string">string</a>) (resp *<a href="index.html#Response">Response</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  837. <p>
  838. Get issues a GET to the specified URL. If the response is one of the
  839. following redirect codes, Get follows the redirect after calling the
  840. Client&#39;s CheckRedirect function:
  841. </p>
  842. <pre>301 (Moved Permanently)
  843. 302 (Found)
  844. 303 (See Other)
  845. 307 (Temporary Redirect)
  846. </pre>
  847. <p>
  848. An error is returned if the Client&#39;s CheckRedirect function fails
  849. or if there was an HTTP protocol error. A non-2xx response doesn&#39;t
  850. cause an error.
  851. </p>
  852. <p>
  853. When err is nil, resp always contains a non-nil resp.Body.
  854. Caller should close resp.Body when done reading from it.
  855. </p>
  856. <p>
  857. To make a request with custom headers, use NewRequest and Client.Do.
  858. </p>
  859. <h3 id="Client.Head">func (*Client) <a href="http://localhost:6060/src/net/http/client.go?s=18570:18631#L600">Head</a></h3>
  860. <pre>func (c *<a href="index.html#Client">Client</a>) Head(url <a href="../../builtin/index.html#string">string</a>) (resp *<a href="index.html#Response">Response</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  861. <p>
  862. Head issues a HEAD to the specified URL. If the response is one of the
  863. following redirect codes, Head follows the redirect after calling the
  864. Client&#39;s CheckRedirect function:
  865. </p>
  866. <pre>301 (Moved Permanently)
  867. 302 (Found)
  868. 303 (See Other)
  869. 307 (Temporary Redirect)
  870. </pre>
  871. <h3 id="Client.Post">func (*Client) <a href="http://localhost:6060/src/net/http/client.go?s=16491:16585#L543">Post</a></h3>
  872. <pre>func (c *<a href="index.html#Client">Client</a>) Post(url <a href="../../builtin/index.html#string">string</a>, bodyType <a href="../../builtin/index.html#string">string</a>, body <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a>) (resp *<a href="index.html#Response">Response</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  873. <p>
  874. Post issues a POST to the specified URL.
  875. </p>
  876. <p>
  877. Caller should close resp.Body when done reading from it.
  878. </p>
  879. <p>
  880. If the provided body is an io.Closer, it is closed after the
  881. request.
  882. </p>
  883. <p>
  884. To set custom headers, use NewRequest and Client.Do.
  885. </p>
  886. <h3 id="Client.PostForm">func (*Client) <a href="http://localhost:6060/src/net/http/client.go?s=17695:17777#L574">PostForm</a></h3>
  887. <pre>func (c *<a href="index.html#Client">Client</a>) PostForm(url <a href="../../builtin/index.html#string">string</a>, data <a href="../url/index.html">url</a>.<a href="../url/index.html#Values">Values</a>) (resp *<a href="index.html#Response">Response</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  888. <p>
  889. PostForm issues a POST to the specified URL,
  890. with data&#39;s keys and values URL-encoded as the request body.
  891. </p>
  892. <p>
  893. The Content-Type header is set to application/x-www-form-urlencoded.
  894. To set other headers, use NewRequest and DefaultClient.Do.
  895. </p>
  896. <p>
  897. When err is nil, resp always contains a non-nil resp.Body.
  898. Caller should close resp.Body when done reading from it.
  899. </p>
  900. <h2 id="CloseNotifier">type <a href="http://localhost:6060/src/net/http/server.go?s=4307:5130#L114">CloseNotifier</a></h2>
  901. <pre>type CloseNotifier interface {
  902. <span class="comment">// CloseNotify returns a channel that receives at most a</span>
  903. <span class="comment">// single value (true) when the client connection has gone</span>
  904. <span class="comment">// away.</span>
  905. <span class="comment">//</span>
  906. <span class="comment">// CloseNotify may wait to notify until Request.Body has been</span>
  907. <span class="comment">// fully read.</span>
  908. <span class="comment">//</span>
  909. <span class="comment">// After the Handler has returned, there is no guarantee</span>
  910. <span class="comment">// that the channel receives a value.</span>
  911. <span class="comment">//</span>
  912. <span class="comment">// If the protocol is HTTP/1.1 and CloseNotify is called while</span>
  913. <span class="comment">// processing an idempotent request (such a GET) while</span>
  914. <span class="comment">// HTTP/1.1 pipelining is in use, the arrival of a subsequent</span>
  915. <span class="comment">// pipelined request may cause a value to be sent on the</span>
  916. <span class="comment">// returned channel. In practice HTTP/1.1 pipelining is not</span>
  917. <span class="comment">// enabled in browsers and not seen often in the wild. If this</span>
  918. <span class="comment">// is a problem, use HTTP/2 or only use CloseNotify on methods</span>
  919. <span class="comment">// such as POST.</span>
  920. CloseNotify() &lt;-chan <a href="../../builtin/index.html#bool">bool</a>
  921. }</pre>
  922. <p>
  923. The CloseNotifier interface is implemented by ResponseWriters which
  924. allow detecting when the underlying connection has gone away.
  925. </p>
  926. <p>
  927. This mechanism can be used to cancel long operations on the server
  928. if the client has disconnected before the response is ready.
  929. </p>
  930. <h2 id="ConnState">type <a href="http://localhost:6060/src/net/http/server.go?s=60475:60493#L2007">ConnState</a></h2>
  931. <pre>type ConnState <a href="../../builtin/index.html#int">int</a></pre>
  932. <p>
  933. A ConnState represents the state of a client connection to a server.
  934. It&#39;s used by the optional Server.ConnState hook.
  935. </p>
  936. <pre>const (
  937. <span class="comment">// StateNew represents a new connection that is expected to</span>
  938. <span class="comment">// send a request immediately. Connections begin at this</span>
  939. <span class="comment">// state and then transition to either StateActive or</span>
  940. <span class="comment">// StateClosed.</span>
  941. <span id="StateNew">StateNew</span> <a href="index.html#ConnState">ConnState</a> = <a href="../../builtin/index.html#iota">iota</a>
  942. <span class="comment">// StateActive represents a connection that has read 1 or more</span>
  943. <span class="comment">// bytes of a request. The Server.ConnState hook for</span>
  944. <span class="comment">// StateActive fires before the request has entered a handler</span>
  945. <span class="comment">// and doesn&#39;t fire again until the request has been</span>
  946. <span class="comment">// handled. After the request is handled, the state</span>
  947. <span class="comment">// transitions to StateClosed, StateHijacked, or StateIdle.</span>
  948. <span class="comment">// For HTTP/2, StateActive fires on the transition from zero</span>
  949. <span class="comment">// to one active request, and only transitions away once all</span>
  950. <span class="comment">// active requests are complete. That means that ConnState</span>
  951. <span class="comment">// can not be used to do per-request work; ConnState only notes</span>
  952. <span class="comment">// the overall state of the connection.</span>
  953. <span id="StateActive">StateActive</span>
  954. <span class="comment">// StateIdle represents a connection that has finished</span>
  955. <span class="comment">// handling a request and is in the keep-alive state, waiting</span>
  956. <span class="comment">// for a new request. Connections transition from StateIdle</span>
  957. <span class="comment">// to either StateActive or StateClosed.</span>
  958. <span id="StateIdle">StateIdle</span>
  959. <span class="comment">// StateHijacked represents a hijacked connection.</span>
  960. <span class="comment">// This is a terminal state. It does not transition to StateClosed.</span>
  961. <span id="StateHijacked">StateHijacked</span>
  962. <span class="comment">// StateClosed represents a closed connection.</span>
  963. <span class="comment">// This is a terminal state. Hijacked connections do not</span>
  964. <span class="comment">// transition to StateClosed.</span>
  965. <span id="StateClosed">StateClosed</span>
  966. )</pre>
  967. <h3 id="ConnState.String">func (ConnState) <a href="http://localhost:6060/src/net/http/server.go?s=62067:62101#L2053">String</a></h3>
  968. <pre>func (c <a href="index.html#ConnState">ConnState</a>) String() <a href="../../builtin/index.html#string">string</a></pre>
  969. <h2 id="Cookie">type <a href="http://localhost:6060/src/net/http/cookie.go?s=439:952#L11">Cookie</a></h2>
  970. <pre>type Cookie struct {
  971. Name <a href="../../builtin/index.html#string">string</a>
  972. Value <a href="../../builtin/index.html#string">string</a>
  973. Path <a href="../../builtin/index.html#string">string</a> <span class="comment">// optional</span>
  974. Domain <a href="../../builtin/index.html#string">string</a> <span class="comment">// optional</span>
  975. Expires <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Time">Time</a> <span class="comment">// optional</span>
  976. RawExpires <a href="../../builtin/index.html#string">string</a> <span class="comment">// for reading cookies only</span>
  977. <span class="comment">// MaxAge=0 means no &#39;Max-Age&#39; attribute specified.</span>
  978. <span class="comment">// MaxAge&lt;0 means delete cookie now, equivalently &#39;Max-Age: 0&#39;</span>
  979. <span class="comment">// MaxAge&gt;0 means Max-Age attribute present and given in seconds</span>
  980. MaxAge <a href="../../builtin/index.html#int">int</a>
  981. Secure <a href="../../builtin/index.html#bool">bool</a>
  982. HttpOnly <a href="../../builtin/index.html#bool">bool</a>
  983. Raw <a href="../../builtin/index.html#string">string</a>
  984. Unparsed []<a href="../../builtin/index.html#string">string</a> <span class="comment">// Raw text of unparsed attribute-value pairs</span>
  985. }</pre>
  986. <p>
  987. A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an
  988. HTTP response or the Cookie header of an HTTP request.
  989. </p>
  990. <p>
  991. See <a href="http://tools.ietf.org/html/rfc6265">http://tools.ietf.org/html/rfc6265</a> for details.
  992. </p>
  993. <h3 id="Cookie.String">func (*Cookie) <a href="http://localhost:6060/src/net/http/cookie.go?s=3428:3460#L130">String</a></h3>
  994. <pre>func (c *<a href="index.html#Cookie">Cookie</a>) String() <a href="../../builtin/index.html#string">string</a></pre>
  995. <p>
  996. String returns the serialization of the cookie for use in a Cookie
  997. header (if only Name and Value are set) or a Set-Cookie response
  998. header (if other fields are set).
  999. If c is nil or c.Name is invalid, the empty string is returned.
  1000. </p>
  1001. <h2 id="CookieJar">type <a href="http://localhost:6060/src/net/http/jar.go?s=433:899#L7">CookieJar</a></h2>
  1002. <pre>type CookieJar interface {
  1003. <span class="comment">// SetCookies handles the receipt of the cookies in a reply for the</span>
  1004. <span class="comment">// given URL. It may or may not choose to save the cookies, depending</span>
  1005. <span class="comment">// on the jar&#39;s policy and implementation.</span>
  1006. SetCookies(u *<a href="../url/index.html">url</a>.<a href="../url/index.html#URL">URL</a>, cookies []*<a href="index.html#Cookie">Cookie</a>)
  1007. <span class="comment">// Cookies returns the cookies to send in a request for the given URL.</span>
  1008. <span class="comment">// It is up to the implementation to honor the standard cookie use</span>
  1009. <span class="comment">// restrictions such as in RFC 6265.</span>
  1010. Cookies(u *<a href="../url/index.html">url</a>.<a href="../url/index.html#URL">URL</a>) []*<a href="index.html#Cookie">Cookie</a>
  1011. }</pre>
  1012. <p>
  1013. A CookieJar manages storage and use of cookies in HTTP requests.
  1014. </p>
  1015. <p>
  1016. Implementations of CookieJar must be safe for concurrent use by multiple
  1017. goroutines.
  1018. </p>
  1019. <p>
  1020. The net/http/cookiejar package provides a CookieJar implementation.
  1021. </p>
  1022. <h2 id="Dir">type <a href="http://localhost:6060/src/net/http/fs.go?s=727:742#L24">Dir</a></h2>
  1023. <pre>type Dir <a href="../../builtin/index.html#string">string</a></pre>
  1024. <p>
  1025. A Dir implements FileSystem using the native file system restricted to a
  1026. specific directory tree.
  1027. </p>
  1028. <p>
  1029. While the FileSystem.Open method takes &#39;/&#39;-separated paths, a Dir&#39;s string
  1030. value is a filename on the native file system, not a URL, so it is separated
  1031. by filepath.Separator, which isn&#39;t necessarily &#39;/&#39;.
  1032. </p>
  1033. <p>
  1034. An empty Dir is treated as &#34;.&#34;.
  1035. </p>
  1036. <h3 id="Dir.Open">func (Dir) <a href="http://localhost:6060/src/net/http/fs.go?s=744:788#L26">Open</a></h3>
  1037. <pre>func (d <a href="index.html#Dir">Dir</a>) Open(name <a href="../../builtin/index.html#string">string</a>) (<a href="index.html#File">File</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  1038. <h2 id="File">type <a href="http://localhost:6060/src/net/http/fs.go?s=1599:1727#L53">File</a></h2>
  1039. <pre>type File interface {
  1040. <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Closer">Closer</a>
  1041. <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a>
  1042. <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Seeker">Seeker</a>
  1043. Readdir(count <a href="../../builtin/index.html#int">int</a>) ([]<a href="../../os/index.html">os</a>.<a href="../../os/index.html#FileInfo">FileInfo</a>, <a href="../../builtin/index.html#error">error</a>)
  1044. Stat() (<a href="../../os/index.html">os</a>.<a href="../../os/index.html#FileInfo">FileInfo</a>, <a href="../../builtin/index.html#error">error</a>)
  1045. }</pre>
  1046. <p>
  1047. A File is returned by a FileSystem&#39;s Open method and can be
  1048. served by the FileServer implementation.
  1049. </p>
  1050. <p>
  1051. The methods should behave the same as those on an *os.File.
  1052. </p>
  1053. <h2 id="FileSystem">type <a href="http://localhost:6060/src/net/http/fs.go?s=1362:1424#L45">FileSystem</a></h2>
  1054. <pre>type FileSystem interface {
  1055. Open(name <a href="../../builtin/index.html#string">string</a>) (<a href="index.html#File">File</a>, <a href="../../builtin/index.html#error">error</a>)
  1056. }</pre>
  1057. <p>
  1058. A FileSystem implements access to a collection of named files.
  1059. The elements in a file path are separated by slash (&#39;/&#39;, U+002F)
  1060. characters, regardless of host operating system convention.
  1061. </p>
  1062. <h2 id="Flusher">type <a href="http://localhost:6060/src/net/http/server.go?s=3297:3381#L87">Flusher</a></h2>
  1063. <pre>type Flusher interface {
  1064. <span class="comment">// Flush sends any buffered data to the client.</span>
  1065. Flush()
  1066. }</pre>
  1067. <p>
  1068. The Flusher interface is implemented by ResponseWriters that allow
  1069. an HTTP handler to flush buffered data to the client.
  1070. </p>
  1071. <p>
  1072. Note that even for ResponseWriters that support Flush,
  1073. if the client is connected through an HTTP proxy,
  1074. the buffered data may not reach the client until the response
  1075. completes.
  1076. </p>
  1077. <h2 id="Handler">type <a href="http://localhost:6060/src/net/http/server.go?s=1646:1709#L47">Handler</a></h2>
  1078. <pre>type Handler interface {
  1079. ServeHTTP(<a href="index.html#ResponseWriter">ResponseWriter</a>, *<a href="index.html#Request">Request</a>)
  1080. }</pre>
  1081. <p>
  1082. A Handler responds to an HTTP request.
  1083. </p>
  1084. <p>
  1085. ServeHTTP should write reply headers and data to the ResponseWriter
  1086. and then return. Returning signals that the request is finished; it
  1087. is not valid to use the ResponseWriter or read from the
  1088. Request.Body after or concurrently with the completion of the
  1089. ServeHTTP call.
  1090. </p>
  1091. <p>
  1092. Depending on the HTTP client software, HTTP protocol version, and
  1093. any intermediaries between the client and the Go server, it may not
  1094. be possible to read from the Request.Body after writing to the
  1095. ResponseWriter. Cautious handlers should read the Request.Body
  1096. first, and then reply.
  1097. </p>
  1098. <p>
  1099. If ServeHTTP panics, the server (the caller of ServeHTTP) assumes
  1100. that the effect of the panic was isolated to the active request.
  1101. It recovers the panic, logs a stack trace to the server error log,
  1102. and hangs up the connection.
  1103. </p>
  1104. <h3 id="FileServer">func <a href="http://localhost:6060/src/net/http/fs.go?s=15736:15776#L497">FileServer</a></h3>
  1105. <pre>func FileServer(root <a href="index.html#FileSystem">FileSystem</a>) <a href="index.html#Handler">Handler</a></pre>
  1106. <p>
  1107. FileServer returns a handler that serves HTTP requests
  1108. with the contents of the file system rooted at root.
  1109. </p>
  1110. <p>
  1111. To use the operating system&#39;s file system implementation,
  1112. use http.Dir:
  1113. </p>
  1114. <pre>http.Handle(&#34;/&#34;, http.FileServer(http.Dir(&#34;/tmp&#34;)))
  1115. </pre>
  1116. <p>
  1117. As a special case, the returned file server redirects any request
  1118. ending in &#34;/index.html&#34; to the same path, without the final
  1119. &#34;index.html&#34;.
  1120. </p>
  1121. <div id="example_FileServer" class="toggle">
  1122. <div class="collapsed">
  1123. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1124. </div>
  1125. <div class="expanded">
  1126. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1127. <p>Code:</p>
  1128. <pre class="code">
  1129. <span class="comment">// Simple static webserver:</span>
  1130. log.Fatal(http.ListenAndServe(&#34;:8080&#34;, http.FileServer(http.Dir(&#34;/usr/share/doc&#34;))))
  1131. </pre>
  1132. </div>
  1133. </div>
  1134. <div id="example_FileServer_stripPrefix" class="toggle">
  1135. <div class="collapsed">
  1136. <p class="exampleHeading toggleButton"><span class="text">Example (StripPrefix)</span></p>
  1137. </div>
  1138. <div class="expanded">
  1139. <p class="exampleHeading toggleButton"><span class="text">Example (StripPrefix)</span></p>
  1140. <p>Code:</p>
  1141. <pre class="code">
  1142. <span class="comment">// To serve a directory on disk (/tmp) under an alternate URL</span>
  1143. <span class="comment">// path (/tmpfiles/), use StripPrefix to modify the request</span>
  1144. <span class="comment">// URL&#39;s path before the FileServer sees it:</span>
  1145. http.Handle(&#34;/tmpfiles/&#34;, http.StripPrefix(&#34;/tmpfiles/&#34;, http.FileServer(http.Dir(&#34;/tmp&#34;))))
  1146. </pre>
  1147. </div>
  1148. </div>
  1149. <h3 id="NotFoundHandler">func <a href="http://localhost:6060/src/net/http/server.go?s=47695:47725#L1627">NotFoundHandler</a></h3>
  1150. <pre>func NotFoundHandler() <a href="index.html#Handler">Handler</a></pre>
  1151. <p>
  1152. NotFoundHandler returns a simple request handler
  1153. that replies to each request with a &ldquo;404 page not found&rdquo; reply.
  1154. </p>
  1155. <h3 id="RedirectHandler">func <a href="http://localhost:6060/src/net/http/server.go?s=51148:51198#L1741">RedirectHandler</a></h3>
  1156. <pre>func RedirectHandler(url <a href="../../builtin/index.html#string">string</a>, code <a href="../../builtin/index.html#int">int</a>) <a href="index.html#Handler">Handler</a></pre>
  1157. <p>
  1158. RedirectHandler returns a request handler that redirects
  1159. each request it receives to the given url using the given
  1160. status code.
  1161. </p>
  1162. <p>
  1163. The provided code should be in the 3xx range and is usually
  1164. StatusMovedPermanently, StatusFound or StatusSeeOther.
  1165. </p>
  1166. <h3 id="StripPrefix">func <a href="http://localhost:6060/src/net/http/server.go?s=48034:48084#L1634">StripPrefix</a></h3>
  1167. <pre>func StripPrefix(prefix <a href="../../builtin/index.html#string">string</a>, h <a href="index.html#Handler">Handler</a>) <a href="index.html#Handler">Handler</a></pre>
  1168. <p>
  1169. StripPrefix returns a handler that serves HTTP requests
  1170. by removing the given prefix from the request URL&#39;s Path
  1171. and invoking the handler h. StripPrefix handles a
  1172. request for a path that doesn&#39;t begin with prefix by
  1173. replying with an HTTP 404 not found error.
  1174. </p>
  1175. <div id="example_StripPrefix" class="toggle">
  1176. <div class="collapsed">
  1177. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1178. </div>
  1179. <div class="expanded">
  1180. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1181. <p>Code:</p>
  1182. <pre class="code">
  1183. <span class="comment">// To serve a directory on disk (/tmp) under an alternate URL</span>
  1184. <span class="comment">// path (/tmpfiles/), use StripPrefix to modify the request</span>
  1185. <span class="comment">// URL&#39;s path before the FileServer sees it:</span>
  1186. http.Handle(&#34;/tmpfiles/&#34;, http.StripPrefix(&#34;/tmpfiles/&#34;, http.FileServer(http.Dir(&#34;/tmp&#34;))))
  1187. </pre>
  1188. </div>
  1189. </div>
  1190. <h3 id="TimeoutHandler">func <a href="http://localhost:6060/src/net/http/server.go?s=69687:69755#L2301">TimeoutHandler</a></h3>
  1191. <pre>func TimeoutHandler(h <a href="index.html#Handler">Handler</a>, dt <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Duration">Duration</a>, msg <a href="../../builtin/index.html#string">string</a>) <a href="index.html#Handler">Handler</a></pre>
  1192. <p>
  1193. TimeoutHandler returns a Handler that runs h with the given time limit.
  1194. </p>
  1195. <p>
  1196. The new Handler calls h.ServeHTTP to handle each request, but if a
  1197. call runs for longer than its time limit, the handler responds with
  1198. a 503 Service Unavailable error and the given message in its body.
  1199. (If msg is empty, a suitable default message will be sent.)
  1200. After such a timeout, writes by h to its ResponseWriter will return
  1201. ErrHandlerTimeout.
  1202. </p>
  1203. <p>
  1204. TimeoutHandler buffers all Handler writes to memory and does not
  1205. support the Hijacker or Flusher interfaces.
  1206. </p>
  1207. <h2 id="HandlerFunc">type <a href="http://localhost:6060/src/net/http/server.go?s=46897:46944#L1604">HandlerFunc</a></h2>
  1208. <pre>type HandlerFunc func(<a href="index.html#ResponseWriter">ResponseWriter</a>, *<a href="index.html#Request">Request</a>)</pre>
  1209. <p>
  1210. The HandlerFunc type is an adapter to allow the use of
  1211. ordinary functions as HTTP handlers. If f is a function
  1212. with the appropriate signature, HandlerFunc(f) is a
  1213. Handler that calls f.
  1214. </p>
  1215. <h3 id="HandlerFunc.ServeHTTP">func (HandlerFunc) <a href="http://localhost:6060/src/net/http/server.go?s=46974:47034#L1607">ServeHTTP</a></h3>
  1216. <pre>func (f <a href="index.html#HandlerFunc">HandlerFunc</a>) ServeHTTP(w <a href="index.html#ResponseWriter">ResponseWriter</a>, r *<a href="index.html#Request">Request</a>)</pre>
  1217. <p>
  1218. ServeHTTP calls f(w, r).
  1219. </p>
  1220. <h2 id="Header">type <a href="http://localhost:6060/src/net/http/header.go?s=350:381#L9">Header</a></h2>
  1221. <pre>type Header map[<a href="../../builtin/index.html#string">string</a>][]<a href="../../builtin/index.html#string">string</a></pre>
  1222. <p>
  1223. A Header represents the key-value pairs in an HTTP header.
  1224. </p>
  1225. <h3 id="Header.Add">func (Header) <a href="http://localhost:6060/src/net/http/header.go?s=488:526#L13">Add</a></h3>
  1226. <pre>func (h <a href="index.html#Header">Header</a>) Add(key, value <a href="../../builtin/index.html#string">string</a>)</pre>
  1227. <p>
  1228. Add adds the key, value pair to the header.
  1229. It appends to any existing values associated with key.
  1230. </p>
  1231. <h3 id="Header.Del">func (Header) <a href="http://localhost:6060/src/net/http/header.go?s=1321:1352#L41">Del</a></h3>
  1232. <pre>func (h <a href="index.html#Header">Header</a>) Del(key <a href="../../builtin/index.html#string">string</a>)</pre>
  1233. <p>
  1234. Del deletes the values associated with key.
  1235. </p>
  1236. <h3 id="Header.Get">func (Header) <a href="http://localhost:6060/src/net/http/header.go?s=1015:1053#L28">Get</a></h3>
  1237. <pre>func (h <a href="index.html#Header">Header</a>) Get(key <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#string">string</a></pre>
  1238. <p>
  1239. Get gets the first value associated with the given key.
  1240. If there are no values associated with the key, Get returns &#34;&#34;.
  1241. To access multiple values of a key, access the map directly
  1242. with CanonicalHeaderKey.
  1243. </p>
  1244. <h3 id="Header.Set">func (Header) <a href="http://localhost:6060/src/net/http/header.go?s=713:751#L20">Set</a></h3>
  1245. <pre>func (h <a href="index.html#Header">Header</a>) Set(key, value <a href="../../builtin/index.html#string">string</a>)</pre>
  1246. <p>
  1247. Set sets the header entries associated with key to
  1248. the single element value. It replaces any existing
  1249. values associated with key.
  1250. </p>
  1251. <h3 id="Header.Write">func (Header) <a href="http://localhost:6060/src/net/http/header.go?s=1433:1473#L46">Write</a></h3>
  1252. <pre>func (h <a href="index.html#Header">Header</a>) Write(w <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Writer">Writer</a>) <a href="../../builtin/index.html#error">error</a></pre>
  1253. <p>
  1254. Write writes a header in wire format.
  1255. </p>
  1256. <h3 id="Header.WriteSubset">func (Header) <a href="http://localhost:6060/src/net/http/header.go?s=3676:3747#L135">WriteSubset</a></h3>
  1257. <pre>func (h <a href="index.html#Header">Header</a>) WriteSubset(w <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Writer">Writer</a>, exclude map[<a href="../../builtin/index.html#string">string</a>]<a href="../../builtin/index.html#bool">bool</a>) <a href="../../builtin/index.html#error">error</a></pre>
  1258. <p>
  1259. WriteSubset writes a header in wire format.
  1260. If exclude is not nil, keys where exclude[key] == true are not written.
  1261. </p>
  1262. <h2 id="Hijacker">type <a href="http://localhost:6060/src/net/http/server.go?s=3502:4032#L94">Hijacker</a></h2>
  1263. <pre>type Hijacker interface {
  1264. <span class="comment">// Hijack lets the caller take over the connection.</span>
  1265. <span class="comment">// After a call to Hijack(), the HTTP server library</span>
  1266. <span class="comment">// will not do anything else with the connection.</span>
  1267. <span class="comment">//</span>
  1268. <span class="comment">// It becomes the caller&#39;s responsibility to manage</span>
  1269. <span class="comment">// and close the connection.</span>
  1270. <span class="comment">//</span>
  1271. <span class="comment">// The returned net.Conn may have read or write deadlines</span>
  1272. <span class="comment">// already set, depending on the configuration of the</span>
  1273. <span class="comment">// Server. It is the caller&#39;s responsibility to set</span>
  1274. <span class="comment">// or clear those deadlines as needed.</span>
  1275. Hijack() (<a href="../index.html">net</a>.<a href="../index.html#Conn">Conn</a>, *<a href="../../bufio/index.html">bufio</a>.<a href="../../bufio/index.html#ReadWriter">ReadWriter</a>, <a href="../../builtin/index.html#error">error</a>)
  1276. }</pre>
  1277. <p>
  1278. The Hijacker interface is implemented by ResponseWriters that allow
  1279. an HTTP handler to take over the connection.
  1280. </p>
  1281. <div id="example_Hijacker" class="toggle">
  1282. <div class="collapsed">
  1283. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1284. </div>
  1285. <div class="expanded">
  1286. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1287. <p>Code:</p>
  1288. <pre class="code">
  1289. http.HandleFunc(&#34;/hijack&#34;, func(w http.ResponseWriter, r *http.Request) {
  1290. hj, ok := w.(http.Hijacker)
  1291. if !ok {
  1292. http.Error(w, &#34;webserver doesn&#39;t support hijacking&#34;, http.StatusInternalServerError)
  1293. return
  1294. }
  1295. conn, bufrw, err := hj.Hijack()
  1296. if err != nil {
  1297. http.Error(w, err.Error(), http.StatusInternalServerError)
  1298. return
  1299. }
  1300. <span class="comment">// Don&#39;t forget to close the connection:</span>
  1301. defer conn.Close()
  1302. bufrw.WriteString(&#34;Now we&#39;re speaking raw TCP. Say hi: &#34;)
  1303. bufrw.Flush()
  1304. s, err := bufrw.ReadString(&#39;\n&#39;)
  1305. if err != nil {
  1306. log.Printf(&#34;error reading string: %v&#34;, err)
  1307. return
  1308. }
  1309. fmt.Fprintf(bufrw, &#34;You said: %q\nBye.\n&#34;, s)
  1310. bufrw.Flush()
  1311. })
  1312. </pre>
  1313. </div>
  1314. </div>
  1315. <h2 id="ProtocolError">type <a href="http://localhost:6060/src/net/http/request.go?s=668:717#L26">ProtocolError</a></h2>
  1316. <pre>type ProtocolError struct {
  1317. ErrorString <a href="../../builtin/index.html#string">string</a>
  1318. }</pre>
  1319. <p>
  1320. HTTP request parsing errors.
  1321. </p>
  1322. <h3 id="ProtocolError.Error">func (*ProtocolError) <a href="http://localhost:6060/src/net/http/request.go?s=719:759#L30">Error</a></h3>
  1323. <pre>func (err *<a href="index.html#ProtocolError">ProtocolError</a>) Error() <a href="../../builtin/index.html#string">string</a></pre>
  1324. <h2 id="Request">type <a href="http://localhost:6060/src/net/http/request.go?s=2057:8999#L64">Request</a></h2>
  1325. <pre>type Request struct {
  1326. <span class="comment">// Method specifies the HTTP method (GET, POST, PUT, etc.).</span>
  1327. <span class="comment">// For client requests an empty string means GET.</span>
  1328. Method <a href="../../builtin/index.html#string">string</a>
  1329. <span class="comment">// URL specifies either the URI being requested (for server</span>
  1330. <span class="comment">// requests) or the URL to access (for client requests).</span>
  1331. <span class="comment">//</span>
  1332. <span class="comment">// For server requests the URL is parsed from the URI</span>
  1333. <span class="comment">// supplied on the Request-Line as stored in RequestURI. For</span>
  1334. <span class="comment">// most requests, fields other than Path and RawQuery will be</span>
  1335. <span class="comment">// empty. (See RFC 2616, Section 5.1.2)</span>
  1336. <span class="comment">//</span>
  1337. <span class="comment">// For client requests, the URL&#39;s Host specifies the server to</span>
  1338. <span class="comment">// connect to, while the Request&#39;s Host field optionally</span>
  1339. <span class="comment">// specifies the Host header value to send in the HTTP</span>
  1340. <span class="comment">// request.</span>
  1341. URL *<a href="../url/index.html">url</a>.<a href="../url/index.html#URL">URL</a>
  1342. <span class="comment">// The protocol version for incoming server requests.</span>
  1343. <span class="comment">//</span>
  1344. <span class="comment">// For client requests these fields are ignored. The HTTP</span>
  1345. <span class="comment">// client code always uses either HTTP/1.1 or HTTP/2.</span>
  1346. <span class="comment">// See the docs on Transport for details.</span>
  1347. Proto <a href="../../builtin/index.html#string">string</a> <span class="comment">// &#34;HTTP/1.0&#34;</span>
  1348. ProtoMajor <a href="../../builtin/index.html#int">int</a> <span class="comment">// 1</span>
  1349. ProtoMinor <a href="../../builtin/index.html#int">int</a> <span class="comment">// 0</span>
  1350. <span class="comment">// Header contains the request header fields either received</span>
  1351. <span class="comment">// by the server or to be sent by the client.</span>
  1352. <span class="comment">//</span>
  1353. <span class="comment">// If a server received a request with header lines,</span>
  1354. <span class="comment">//</span>
  1355. <span class="comment">// Host: example.com</span>
  1356. <span class="comment">// accept-encoding: gzip, deflate</span>
  1357. <span class="comment">// Accept-Language: en-us</span>
  1358. <span class="comment">// fOO: Bar</span>
  1359. <span class="comment">// foo: two</span>
  1360. <span class="comment">//</span>
  1361. <span class="comment">// then</span>
  1362. <span class="comment">//</span>
  1363. <span class="comment">// Header = map[string][]string{</span>
  1364. <span class="comment">// &#34;Accept-Encoding&#34;: {&#34;gzip, deflate&#34;},</span>
  1365. <span class="comment">// &#34;Accept-Language&#34;: {&#34;en-us&#34;},</span>
  1366. <span class="comment">// &#34;Foo&#34;: {&#34;Bar&#34;, &#34;two&#34;},</span>
  1367. <span class="comment">// }</span>
  1368. <span class="comment">//</span>
  1369. <span class="comment">// For incoming requests, the Host header is promoted to the</span>
  1370. <span class="comment">// Request.Host field and removed from the Header map.</span>
  1371. <span class="comment">//</span>
  1372. <span class="comment">// HTTP defines that header names are case-insensitive. The</span>
  1373. <span class="comment">// request parser implements this by using CanonicalHeaderKey,</span>
  1374. <span class="comment">// making the first character and any characters following a</span>
  1375. <span class="comment">// hyphen uppercase and the rest lowercase.</span>
  1376. <span class="comment">//</span>
  1377. <span class="comment">// For client requests, certain headers such as Content-Length</span>
  1378. <span class="comment">// and Connection are automatically written when needed and</span>
  1379. <span class="comment">// values in Header may be ignored. See the documentation</span>
  1380. <span class="comment">// for the Request.Write method.</span>
  1381. Header <a href="index.html#Header">Header</a>
  1382. <span class="comment">// Body is the request&#39;s body.</span>
  1383. <span class="comment">//</span>
  1384. <span class="comment">// For client requests a nil body means the request has no</span>
  1385. <span class="comment">// body, such as a GET request. The HTTP Client&#39;s Transport</span>
  1386. <span class="comment">// is responsible for calling the Close method.</span>
  1387. <span class="comment">//</span>
  1388. <span class="comment">// For server requests the Request Body is always non-nil</span>
  1389. <span class="comment">// but will return EOF immediately when no body is present.</span>
  1390. <span class="comment">// The Server will close the request body. The ServeHTTP</span>
  1391. <span class="comment">// Handler does not need to.</span>
  1392. Body <a href="../../io/index.html">io</a>.<a href="../../io/index.html#ReadCloser">ReadCloser</a>
  1393. <span class="comment">// ContentLength records the length of the associated content.</span>
  1394. <span class="comment">// The value -1 indicates that the length is unknown.</span>
  1395. <span class="comment">// Values &gt;= 0 indicate that the given number of bytes may</span>
  1396. <span class="comment">// be read from Body.</span>
  1397. <span class="comment">// For client requests, a value of 0 means unknown if Body is not nil.</span>
  1398. ContentLength <a href="../../builtin/index.html#int64">int64</a>
  1399. <span class="comment">// TransferEncoding lists the transfer encodings from outermost to</span>
  1400. <span class="comment">// innermost. An empty list denotes the &#34;identity&#34; encoding.</span>
  1401. <span class="comment">// TransferEncoding can usually be ignored; chunked encoding is</span>
  1402. <span class="comment">// automatically added and removed as necessary when sending and</span>
  1403. <span class="comment">// receiving requests.</span>
  1404. TransferEncoding []<a href="../../builtin/index.html#string">string</a>
  1405. <span class="comment">// Close indicates whether to close the connection after</span>
  1406. <span class="comment">// replying to this request (for servers) or after sending this</span>
  1407. <span class="comment">// request and reading its response (for clients).</span>
  1408. <span class="comment">//</span>
  1409. <span class="comment">// For server requests, the HTTP server handles this automatically</span>
  1410. <span class="comment">// and this field is not needed by Handlers.</span>
  1411. <span class="comment">//</span>
  1412. <span class="comment">// For client requests, setting this field prevents re-use of</span>
  1413. <span class="comment">// TCP connections between requests to the same hosts, as if</span>
  1414. <span class="comment">// Transport.DisableKeepAlives were set.</span>
  1415. Close <a href="../../builtin/index.html#bool">bool</a>
  1416. <span class="comment">// For server requests Host specifies the host on which the</span>
  1417. <span class="comment">// URL is sought. Per RFC 2616, this is either the value of</span>
  1418. <span class="comment">// the &#34;Host&#34; header or the host name given in the URL itself.</span>
  1419. <span class="comment">// It may be of the form &#34;host:port&#34;.</span>
  1420. <span class="comment">//</span>
  1421. <span class="comment">// For client requests Host optionally overrides the Host</span>
  1422. <span class="comment">// header to send. If empty, the Request.Write method uses</span>
  1423. <span class="comment">// the value of URL.Host.</span>
  1424. Host <a href="../../builtin/index.html#string">string</a>
  1425. <span class="comment">// Form contains the parsed form data, including both the URL</span>
  1426. <span class="comment">// field&#39;s query parameters and the POST or PUT form data.</span>
  1427. <span class="comment">// This field is only available after ParseForm is called.</span>
  1428. <span class="comment">// The HTTP client ignores Form and uses Body instead.</span>
  1429. Form <a href="../url/index.html">url</a>.<a href="../url/index.html#Values">Values</a>
  1430. <span class="comment">// PostForm contains the parsed form data from POST, PATCH,</span>
  1431. <span class="comment">// or PUT body parameters.</span>
  1432. <span class="comment">//</span>
  1433. <span class="comment">// This field is only available after ParseForm is called.</span>
  1434. <span class="comment">// The HTTP client ignores PostForm and uses Body instead.</span>
  1435. PostForm <a href="../url/index.html">url</a>.<a href="../url/index.html#Values">Values</a>
  1436. <span class="comment">// MultipartForm is the parsed multipart form, including file uploads.</span>
  1437. <span class="comment">// This field is only available after ParseMultipartForm is called.</span>
  1438. <span class="comment">// The HTTP client ignores MultipartForm and uses Body instead.</span>
  1439. MultipartForm *<a href="../../mime/multipart/index.html">multipart</a>.<a href="../../mime/multipart/index.html#Form">Form</a>
  1440. <span class="comment">// Trailer specifies additional headers that are sent after the request</span>
  1441. <span class="comment">// body.</span>
  1442. <span class="comment">//</span>
  1443. <span class="comment">// For server requests the Trailer map initially contains only the</span>
  1444. <span class="comment">// trailer keys, with nil values. (The client declares which trailers it</span>
  1445. <span class="comment">// will later send.) While the handler is reading from Body, it must</span>
  1446. <span class="comment">// not reference Trailer. After reading from Body returns EOF, Trailer</span>
  1447. <span class="comment">// can be read again and will contain non-nil values, if they were sent</span>
  1448. <span class="comment">// by the client.</span>
  1449. <span class="comment">//</span>
  1450. <span class="comment">// For client requests Trailer must be initialized to a map containing</span>
  1451. <span class="comment">// the trailer keys to later send. The values may be nil or their final</span>
  1452. <span class="comment">// values. The ContentLength must be 0 or -1, to send a chunked request.</span>
  1453. <span class="comment">// After the HTTP request is sent the map values can be updated while</span>
  1454. <span class="comment">// the request body is read. Once the body returns EOF, the caller must</span>
  1455. <span class="comment">// not mutate Trailer.</span>
  1456. <span class="comment">//</span>
  1457. <span class="comment">// Few HTTP clients, servers, or proxies support HTTP trailers.</span>
  1458. Trailer <a href="index.html#Header">Header</a>
  1459. <span class="comment">// RemoteAddr allows HTTP servers and other software to record</span>
  1460. <span class="comment">// the network address that sent the request, usually for</span>
  1461. <span class="comment">// logging. This field is not filled in by ReadRequest and</span>
  1462. <span class="comment">// has no defined format. The HTTP server in this package</span>
  1463. <span class="comment">// sets RemoteAddr to an &#34;IP:port&#34; address before invoking a</span>
  1464. <span class="comment">// handler.</span>
  1465. <span class="comment">// This field is ignored by the HTTP client.</span>
  1466. RemoteAddr <a href="../../builtin/index.html#string">string</a>
  1467. <span class="comment">// RequestURI is the unmodified Request-URI of the</span>
  1468. <span class="comment">// Request-Line (RFC 2616, Section 5.1) as sent by the client</span>
  1469. <span class="comment">// to a server. Usually the URL field should be used instead.</span>
  1470. <span class="comment">// It is an error to set this field in an HTTP client request.</span>
  1471. RequestURI <a href="../../builtin/index.html#string">string</a>
  1472. <span class="comment">// TLS allows HTTP servers and other software to record</span>
  1473. <span class="comment">// information about the TLS connection on which the request</span>
  1474. <span class="comment">// was received. This field is not filled in by ReadRequest.</span>
  1475. <span class="comment">// The HTTP server in this package sets the field for</span>
  1476. <span class="comment">// TLS-enabled connections before invoking a handler;</span>
  1477. <span class="comment">// otherwise it leaves the field nil.</span>
  1478. <span class="comment">// This field is ignored by the HTTP client.</span>
  1479. TLS *<a href="../../crypto/tls/index.html">tls</a>.<a href="../../crypto/tls/index.html#ConnectionState">ConnectionState</a>
  1480. <span class="comment">// Cancel is an optional channel whose closure indicates that the client</span>
  1481. <span class="comment">// request should be regarded as canceled. Not all implementations of</span>
  1482. <span class="comment">// RoundTripper may support Cancel.</span>
  1483. <span class="comment">//</span>
  1484. <span class="comment">// For server requests, this field is not applicable.</span>
  1485. Cancel &lt;-chan struct{}
  1486. }</pre>
  1487. <p>
  1488. A Request represents an HTTP request received by a server
  1489. or to be sent by a client.
  1490. </p>
  1491. <p>
  1492. The field semantics differ slightly between client and server
  1493. usage. In addition to the notes on the fields below, see the
  1494. documentation for Request.Write and RoundTripper.
  1495. </p>
  1496. <h3 id="NewRequest">func <a href="http://localhost:6060/src/net/http/request.go?s=19631:19703#L588">NewRequest</a></h3>
  1497. <pre>func NewRequest(method, urlStr <a href="../../builtin/index.html#string">string</a>, body <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a>) (*<a href="index.html#Request">Request</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  1498. <p>
  1499. NewRequest returns a new Request given a method, URL, and optional body.
  1500. </p>
  1501. <p>
  1502. If the provided body is also an io.Closer, the returned
  1503. Request.Body is set to body and will be closed by the Client
  1504. methods Do, Post, and PostForm, and Transport.RoundTrip.
  1505. </p>
  1506. <p>
  1507. NewRequest returns a Request suitable for use with Client.Do or
  1508. Transport.RoundTrip.
  1509. To create a request for use with testing a Server Handler use either
  1510. ReadRequest or manually update the Request fields. See the Request
  1511. type&#39;s documentation for the difference between inbound and outbound
  1512. request fields.
  1513. </p>
  1514. <h3 id="ReadRequest">func <a href="http://localhost:6060/src/net/http/request.go?s=22536:22595#L697">ReadRequest</a></h3>
  1515. <pre>func ReadRequest(b *<a href="../../bufio/index.html">bufio</a>.<a href="../../bufio/index.html#Reader">Reader</a>) (req *<a href="index.html#Request">Request</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  1516. <p>
  1517. ReadRequest reads and parses an incoming request from b.
  1518. </p>
  1519. <h3 id="Request.AddCookie">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=10152:10190#L276">AddCookie</a></h3>
  1520. <pre>func (r *<a href="index.html#Request">Request</a>) AddCookie(c *<a href="index.html#Cookie">Cookie</a>)</pre>
  1521. <p>
  1522. AddCookie adds a cookie to the request. Per RFC 6265 section 5.4,
  1523. AddCookie does not attach more than one Cookie header field. That
  1524. means all cookies, if any, are written into the same line,
  1525. separated by semicolon.
  1526. </p>
  1527. <h3 id="Request.BasicAuth">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=20802:20868#L633">BasicAuth</a></h3>
  1528. <pre>func (r *<a href="index.html#Request">Request</a>) BasicAuth() (username, password <a href="../../builtin/index.html#string">string</a>, ok <a href="../../builtin/index.html#bool">bool</a>)</pre>
  1529. <p>
  1530. BasicAuth returns the username and password provided in the request&#39;s
  1531. Authorization header, if the request uses HTTP Basic Authentication.
  1532. See RFC 2617, Section 2.
  1533. </p>
  1534. <h3 id="Request.Cookie">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=9770:9824#L265">Cookie</a></h3>
  1535. <pre>func (r *<a href="index.html#Request">Request</a>) Cookie(name <a href="../../builtin/index.html#string">string</a>) (*<a href="index.html#Cookie">Cookie</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  1536. <p>
  1537. Cookie returns the named cookie provided in the request or
  1538. ErrNoCookie if not found.
  1539. </p>
  1540. <h3 id="Request.Cookies">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=9456:9493#L256">Cookies</a></h3>
  1541. <pre>func (r *<a href="index.html#Request">Request</a>) Cookies() []*<a href="index.html#Cookie">Cookie</a></pre>
  1542. <p>
  1543. Cookies parses and returns the HTTP cookies sent with the request.
  1544. </p>
  1545. <h3 id="Request.FormFile">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=31586:31671#L1030">FormFile</a></h3>
  1546. <pre>func (r *<a href="index.html#Request">Request</a>) FormFile(key <a href="../../builtin/index.html#string">string</a>) (<a href="../../mime/multipart/index.html">multipart</a>.<a href="../../mime/multipart/index.html#File">File</a>, *<a href="../../mime/multipart/index.html">multipart</a>.<a href="../../mime/multipart/index.html#FileHeader">FileHeader</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  1547. <p>
  1548. FormFile returns the first file for the provided form key.
  1549. FormFile calls ParseMultipartForm and ParseForm if necessary.
  1550. </p>
  1551. <h3 id="Request.FormValue">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=30758:30804#L1003">FormValue</a></h3>
  1552. <pre>func (r *<a href="index.html#Request">Request</a>) FormValue(key <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#string">string</a></pre>
  1553. <p>
  1554. FormValue returns the first value for the named component of the query.
  1555. POST and PUT body parameters take precedence over URL query string values.
  1556. FormValue calls ParseMultipartForm and ParseForm if necessary and ignores
  1557. any errors returned by these functions.
  1558. If key is not present, FormValue returns the empty string.
  1559. To access multiple values of the same key, call ParseForm and
  1560. then inspect Request.Form directly.
  1561. </p>
  1562. <h3 id="Request.MultipartReader">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=11489:11551#L309">MultipartReader</a></h3>
  1563. <pre>func (r *<a href="index.html#Request">Request</a>) MultipartReader() (*<a href="../../mime/multipart/index.html">multipart</a>.<a href="../../mime/multipart/index.html#Reader">Reader</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  1564. <p>
  1565. MultipartReader returns a MIME multipart reader if this is a
  1566. multipart/form-data POST request, else returns nil and an error.
  1567. Use this function instead of ParseMultipartForm to
  1568. process the request body as a stream.
  1569. </p>
  1570. <h3 id="Request.ParseForm">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=28722:28757#L924">ParseForm</a></h3>
  1571. <pre>func (r *<a href="index.html#Request">Request</a>) ParseForm() <a href="../../builtin/index.html#error">error</a></pre>
  1572. <p>
  1573. ParseForm parses the raw query from the URL and updates r.Form.
  1574. </p>
  1575. <p>
  1576. For POST or PUT requests, it also parses the request body as a form and
  1577. put the results into both r.PostForm and r.Form.
  1578. POST and PUT body parameters take precedence over URL query string values
  1579. in r.Form.
  1580. </p>
  1581. <p>
  1582. If the request Body&#39;s size has not already been limited by MaxBytesReader,
  1583. the size is capped at 10MB.
  1584. </p>
  1585. <p>
  1586. ParseMultipartForm calls ParseForm automatically.
  1587. It is idempotent.
  1588. </p>
  1589. <h3 id="Request.ParseMultipartForm">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=29777:29836#L965">ParseMultipartForm</a></h3>
  1590. <pre>func (r *<a href="index.html#Request">Request</a>) ParseMultipartForm(maxMemory <a href="../../builtin/index.html#int64">int64</a>) <a href="../../builtin/index.html#error">error</a></pre>
  1591. <p>
  1592. ParseMultipartForm parses a request body as multipart/form-data.
  1593. The whole request body is parsed and up to a total of maxMemory bytes of
  1594. its file parts are stored in memory, with the remainder stored on
  1595. disk in temporary files.
  1596. ParseMultipartForm calls ParseForm if necessary.
  1597. After one call to ParseMultipartForm, subsequent calls have no effect.
  1598. </p>
  1599. <h3 id="Request.PostFormValue">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=31265:31315#L1018">PostFormValue</a></h3>
  1600. <pre>func (r *<a href="index.html#Request">Request</a>) PostFormValue(key <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#string">string</a></pre>
  1601. <p>
  1602. PostFormValue returns the first value for the named component of the POST
  1603. or PUT request body. URL query parameters are ignored.
  1604. PostFormValue calls ParseMultipartForm and ParseForm if necessary and ignores
  1605. any errors returned by these functions.
  1606. If key is not present, PostFormValue returns the empty string.
  1607. </p>
  1608. <h3 id="Request.ProtoAtLeast">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=9099:9152#L245">ProtoAtLeast</a></h3>
  1609. <pre>func (r *<a href="index.html#Request">Request</a>) ProtoAtLeast(major, minor <a href="../../builtin/index.html#int">int</a>) <a href="../../builtin/index.html#bool">bool</a></pre>
  1610. <p>
  1611. ProtoAtLeast reports whether the HTTP protocol used
  1612. in the request is at least major.minor.
  1613. </p>
  1614. <h3 id="Request.Referer">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=10860:10894#L293">Referer</a></h3>
  1615. <pre>func (r *<a href="index.html#Request">Request</a>) Referer() <a href="../../builtin/index.html#string">string</a></pre>
  1616. <p>
  1617. Referer returns the referring URL, if sent in the request.
  1618. </p>
  1619. <p>
  1620. Referer is misspelled as in the request itself, a mistake from the
  1621. earliest days of HTTP. This value can also be fetched from the
  1622. Header map as Header[&#34;Referer&#34;]; the benefit of making it available
  1623. as a method is that the compiler can diagnose programs that use the
  1624. alternate (correct English) spelling req.Referrer() but cannot
  1625. diagnose programs that use Header[&#34;Referrer&#34;].
  1626. </p>
  1627. <h3 id="Request.SetBasicAuth">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=21690:21747#L665">SetBasicAuth</a></h3>
  1628. <pre>func (r *<a href="index.html#Request">Request</a>) SetBasicAuth(username, password <a href="../../builtin/index.html#string">string</a>)</pre>
  1629. <p>
  1630. SetBasicAuth sets the request&#39;s Authorization header to use HTTP
  1631. Basic Authentication with the provided username and password.
  1632. </p>
  1633. <p>
  1634. With HTTP Basic Authentication the provided username and password
  1635. are not encrypted.
  1636. </p>
  1637. <h3 id="Request.UserAgent">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=9309:9345#L251">UserAgent</a></h3>
  1638. <pre>func (r *<a href="index.html#Request">Request</a>) UserAgent() <a href="../../builtin/index.html#string">string</a></pre>
  1639. <p>
  1640. UserAgent returns the client&#39;s User-Agent, if sent in the request.
  1641. </p>
  1642. <h3 id="Request.Write">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=13140:13182#L363">Write</a></h3>
  1643. <pre>func (r *<a href="index.html#Request">Request</a>) Write(w <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Writer">Writer</a>) <a href="../../builtin/index.html#error">error</a></pre>
  1644. <p>
  1645. Write writes an HTTP/1.1 request, which is the header and body, in wire format.
  1646. This method consults the following fields of the request:
  1647. </p>
  1648. <pre>Host
  1649. URL
  1650. Method (defaults to &#34;GET&#34;)
  1651. Header
  1652. ContentLength
  1653. TransferEncoding
  1654. Body
  1655. </pre>
  1656. <p>
  1657. If Body is present, Content-Length is &lt;= 0 and TransferEncoding
  1658. hasn&#39;t been set to &#34;identity&#34;, Write adds &#34;Transfer-Encoding:
  1659. chunked&#34; to the header. Body is closed after it is sent.
  1660. </p>
  1661. <h3 id="Request.WriteProxy">func (*Request) <a href="http://localhost:6060/src/net/http/request.go?s=13580:13627#L373">WriteProxy</a></h3>
  1662. <pre>func (r *<a href="index.html#Request">Request</a>) WriteProxy(w <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Writer">Writer</a>) <a href="../../builtin/index.html#error">error</a></pre>
  1663. <p>
  1664. WriteProxy is like Write but writes the request in the form
  1665. expected by an HTTP proxy. In particular, WriteProxy writes the
  1666. initial Request-URI line of the request with an absolute URI, per
  1667. section 5.1.2 of RFC 2616, including the scheme and host.
  1668. In either case, WriteProxy also writes a Host header, using
  1669. either r.Host or r.URL.Host.
  1670. </p>
  1671. <h2 id="Response">type <a href="http://localhost:6060/src/net/http/response.go?s=512:3205#L19">Response</a></h2>
  1672. <pre>type Response struct {
  1673. Status <a href="../../builtin/index.html#string">string</a> <span class="comment">// e.g. &#34;200 OK&#34;</span>
  1674. StatusCode <a href="../../builtin/index.html#int">int</a> <span class="comment">// e.g. 200</span>
  1675. Proto <a href="../../builtin/index.html#string">string</a> <span class="comment">// e.g. &#34;HTTP/1.0&#34;</span>
  1676. ProtoMajor <a href="../../builtin/index.html#int">int</a> <span class="comment">// e.g. 1</span>
  1677. ProtoMinor <a href="../../builtin/index.html#int">int</a> <span class="comment">// e.g. 0</span>
  1678. <span class="comment">// Header maps header keys to values. If the response had multiple</span>
  1679. <span class="comment">// headers with the same key, they may be concatenated, with comma</span>
  1680. <span class="comment">// delimiters. (Section 4.2 of RFC 2616 requires that multiple headers</span>
  1681. <span class="comment">// be semantically equivalent to a comma-delimited sequence.) Values</span>
  1682. <span class="comment">// duplicated by other fields in this struct (e.g., ContentLength) are</span>
  1683. <span class="comment">// omitted from Header.</span>
  1684. <span class="comment">//</span>
  1685. <span class="comment">// Keys in the map are canonicalized (see CanonicalHeaderKey).</span>
  1686. Header <a href="index.html#Header">Header</a>
  1687. <span class="comment">// Body represents the response body.</span>
  1688. <span class="comment">//</span>
  1689. <span class="comment">// The http Client and Transport guarantee that Body is always</span>
  1690. <span class="comment">// non-nil, even on responses without a body or responses with</span>
  1691. <span class="comment">// a zero-length body. It is the caller&#39;s responsibility to</span>
  1692. <span class="comment">// close Body. The default HTTP client&#39;s Transport does not</span>
  1693. <span class="comment">// attempt to reuse HTTP/1.0 or HTTP/1.1 TCP connections</span>
  1694. <span class="comment">// (&#34;keep-alive&#34;) unless the Body is read to completion and is</span>
  1695. <span class="comment">// closed.</span>
  1696. <span class="comment">//</span>
  1697. <span class="comment">// The Body is automatically dechunked if the server replied</span>
  1698. <span class="comment">// with a &#34;chunked&#34; Transfer-Encoding.</span>
  1699. Body <a href="../../io/index.html">io</a>.<a href="../../io/index.html#ReadCloser">ReadCloser</a>
  1700. <span class="comment">// ContentLength records the length of the associated content. The</span>
  1701. <span class="comment">// value -1 indicates that the length is unknown. Unless Request.Method</span>
  1702. <span class="comment">// is &#34;HEAD&#34;, values &gt;= 0 indicate that the given number of bytes may</span>
  1703. <span class="comment">// be read from Body.</span>
  1704. ContentLength <a href="../../builtin/index.html#int64">int64</a>
  1705. <span class="comment">// Contains transfer encodings from outer-most to inner-most. Value is</span>
  1706. <span class="comment">// nil, means that &#34;identity&#34; encoding is used.</span>
  1707. TransferEncoding []<a href="../../builtin/index.html#string">string</a>
  1708. <span class="comment">// Close records whether the header directed that the connection be</span>
  1709. <span class="comment">// closed after reading Body. The value is advice for clients: neither</span>
  1710. <span class="comment">// ReadResponse nor Response.Write ever closes a connection.</span>
  1711. Close <a href="../../builtin/index.html#bool">bool</a>
  1712. <span class="comment">// Trailer maps trailer keys to values in the same</span>
  1713. <span class="comment">// format as Header.</span>
  1714. <span class="comment">//</span>
  1715. <span class="comment">// The Trailer initially contains only nil values, one for</span>
  1716. <span class="comment">// each key specified in the server&#39;s &#34;Trailer&#34; header</span>
  1717. <span class="comment">// value. Those values are not added to Header.</span>
  1718. <span class="comment">//</span>
  1719. <span class="comment">// Trailer must not be accessed concurrently with Read calls</span>
  1720. <span class="comment">// on the Body.</span>
  1721. <span class="comment">//</span>
  1722. <span class="comment">// After Body.Read has returned io.EOF, Trailer will contain</span>
  1723. <span class="comment">// any trailer values sent by the server.</span>
  1724. Trailer <a href="index.html#Header">Header</a>
  1725. <span class="comment">// The Request that was sent to obtain this Response.</span>
  1726. <span class="comment">// Request&#39;s Body is nil (having already been consumed).</span>
  1727. <span class="comment">// This is only populated for Client requests.</span>
  1728. Request *<a href="index.html#Request">Request</a>
  1729. <span class="comment">// TLS contains information about the TLS connection on which the</span>
  1730. <span class="comment">// response was received. It is nil for unencrypted responses.</span>
  1731. <span class="comment">// The pointer is shared between responses and should not be</span>
  1732. <span class="comment">// modified.</span>
  1733. TLS *<a href="../../crypto/tls/index.html">tls</a>.<a href="../../crypto/tls/index.html#ConnectionState">ConnectionState</a>
  1734. }</pre>
  1735. <p>
  1736. Response represents the response from an HTTP request.
  1737. </p>
  1738. <h3 id="Get">func <a href="http://localhost:6060/src/net/http/client.go?s=12208:12256#L386">Get</a></h3>
  1739. <pre>func Get(url <a href="../../builtin/index.html#string">string</a>) (resp *<a href="index.html#Response">Response</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  1740. <p>
  1741. Get issues a GET to the specified URL. If the response is one of
  1742. the following redirect codes, Get follows the redirect, up to a
  1743. maximum of 10 redirects:
  1744. </p>
  1745. <pre>301 (Moved Permanently)
  1746. 302 (Found)
  1747. 303 (See Other)
  1748. 307 (Temporary Redirect)
  1749. </pre>
  1750. <p>
  1751. An error is returned if there were too many redirects or if there
  1752. was an HTTP protocol error. A non-2xx response doesn&#39;t cause an
  1753. error.
  1754. </p>
  1755. <p>
  1756. When err is nil, resp always contains a non-nil resp.Body.
  1757. Caller should close resp.Body when done reading from it.
  1758. </p>
  1759. <p>
  1760. Get is a wrapper around DefaultClient.Get.
  1761. </p>
  1762. <p>
  1763. To make a request with custom headers, use NewRequest and
  1764. DefaultClient.Do.
  1765. </p>
  1766. <div id="example_Get" class="toggle">
  1767. <div class="collapsed">
  1768. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1769. </div>
  1770. <div class="expanded">
  1771. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  1772. <p>Code:</p>
  1773. <pre class="code">
  1774. res, err := http.Get(&#34;http://www.google.com/robots.txt&#34;)
  1775. if err != nil {
  1776. log.Fatal(err)
  1777. }
  1778. robots, err := ioutil.ReadAll(res.Body)
  1779. res.Body.Close()
  1780. if err != nil {
  1781. log.Fatal(err)
  1782. }
  1783. fmt.Printf(&#34;%s&#34;, robots)
  1784. </pre>
  1785. </div>
  1786. </div>
  1787. <h3 id="Head">func <a href="http://localhost:6060/src/net/http/client.go?s=18195:18244#L588">Head</a></h3>
  1788. <pre>func Head(url <a href="../../builtin/index.html#string">string</a>) (resp *<a href="index.html#Response">Response</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  1789. <p>
  1790. Head issues a HEAD to the specified URL. If the response is one of
  1791. the following redirect codes, Head follows the redirect, up to a
  1792. maximum of 10 redirects:
  1793. </p>
  1794. <pre>301 (Moved Permanently)
  1795. 302 (Found)
  1796. 303 (See Other)
  1797. 307 (Temporary Redirect)
  1798. </pre>
  1799. <p>
  1800. Head is a wrapper around DefaultClient.Head
  1801. </p>
  1802. <h3 id="Post">func <a href="http://localhost:6060/src/net/http/client.go?s=16110:16192#L531">Post</a></h3>
  1803. <pre>func Post(url <a href="../../builtin/index.html#string">string</a>, bodyType <a href="../../builtin/index.html#string">string</a>, body <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Reader">Reader</a>) (resp *<a href="index.html#Response">Response</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  1804. <p>
  1805. Post issues a POST to the specified URL.
  1806. </p>
  1807. <p>
  1808. Caller should close resp.Body when done reading from it.
  1809. </p>
  1810. <p>
  1811. If the provided body is an io.Closer, it is closed after the
  1812. request.
  1813. </p>
  1814. <p>
  1815. Post is a wrapper around DefaultClient.Post.
  1816. </p>
  1817. <p>
  1818. To set custom headers, use NewRequest and DefaultClient.Do.
  1819. </p>
  1820. <h3 id="PostForm">func <a href="http://localhost:6060/src/net/http/client.go?s=17203:17273#L562">PostForm</a></h3>
  1821. <pre>func PostForm(url <a href="../../builtin/index.html#string">string</a>, data <a href="../url/index.html">url</a>.<a href="../url/index.html#Values">Values</a>) (resp *<a href="index.html#Response">Response</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  1822. <p>
  1823. PostForm issues a POST to the specified URL, with data&#39;s keys and
  1824. values URL-encoded as the request body.
  1825. </p>
  1826. <p>
  1827. The Content-Type header is set to application/x-www-form-urlencoded.
  1828. To set other headers, use NewRequest and DefaultClient.Do.
  1829. </p>
  1830. <p>
  1831. When err is nil, resp always contains a non-nil resp.Body.
  1832. Caller should close resp.Body when done reading from it.
  1833. </p>
  1834. <p>
  1835. PostForm is a wrapper around DefaultClient.PostForm.
  1836. </p>
  1837. <h3 id="ReadResponse">func <a href="http://localhost:6060/src/net/http/response.go?s=4348:4415#L121">ReadResponse</a></h3>
  1838. <pre>func ReadResponse(r *<a href="../../bufio/index.html">bufio</a>.<a href="../../bufio/index.html#Reader">Reader</a>, req *<a href="index.html#Request">Request</a>) (*<a href="index.html#Response">Response</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  1839. <p>
  1840. ReadResponse reads and returns an HTTP response from r.
  1841. The req parameter optionally specifies the Request that corresponds
  1842. to this Response. If nil, a GET request is assumed.
  1843. Clients must call resp.Body.Close when finished reading resp.Body.
  1844. After that call, clients can inspect resp.Trailer to find key/value
  1845. pairs included in the response trailer.
  1846. </p>
  1847. <h3 id="Response.Cookies">func (*Response) <a href="http://localhost:6060/src/net/http/response.go?s=3280:3318#L92">Cookies</a></h3>
  1848. <pre>func (r *<a href="index.html#Response">Response</a>) Cookies() []*<a href="index.html#Cookie">Cookie</a></pre>
  1849. <p>
  1850. Cookies parses and returns the cookies set in the Set-Cookie headers.
  1851. </p>
  1852. <h3 id="Response.Location">func (*Response) <a href="http://localhost:6060/src/net/http/response.go?s=3743:3790#L104">Location</a></h3>
  1853. <pre>func (r *<a href="index.html#Response">Response</a>) Location() (*<a href="../url/index.html">url</a>.<a href="../url/index.html#URL">URL</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  1854. <p>
  1855. Location returns the URL of the response&#39;s &#34;Location&#34; header,
  1856. if present. Relative redirects are resolved relative to
  1857. the Response&#39;s Request. ErrNoLocation is returned if no
  1858. Location header is present.
  1859. </p>
  1860. <h3 id="Response.ProtoAtLeast">func (*Response) <a href="http://localhost:6060/src/net/http/response.go?s=6038:6092#L191">ProtoAtLeast</a></h3>
  1861. <pre>func (r *<a href="index.html#Response">Response</a>) ProtoAtLeast(major, minor <a href="../../builtin/index.html#int">int</a>) <a href="../../builtin/index.html#bool">bool</a></pre>
  1862. <p>
  1863. ProtoAtLeast reports whether the HTTP protocol used
  1864. in the response is at least major.minor.
  1865. </p>
  1866. <h3 id="Response.Write">func (*Response) <a href="http://localhost:6060/src/net/http/response.go?s=6630:6673#L212">Write</a></h3>
  1867. <pre>func (r *<a href="index.html#Response">Response</a>) Write(w <a href="../../io/index.html">io</a>.<a href="../../io/index.html#Writer">Writer</a>) <a href="../../builtin/index.html#error">error</a></pre>
  1868. <p>
  1869. Write writes r to w in the HTTP/1.n server response format,
  1870. including the status line, headers, body, and optional trailer.
  1871. </p>
  1872. <p>
  1873. This method consults the following fields of the response r:
  1874. </p>
  1875. <pre>StatusCode
  1876. ProtoMajor
  1877. ProtoMinor
  1878. Request.Method
  1879. TransferEncoding
  1880. Trailer
  1881. Body
  1882. ContentLength
  1883. Header, values for non-canonical keys will have unpredictable behavior
  1884. </pre>
  1885. <p>
  1886. The Response Body is closed after it is sent.
  1887. </p>
  1888. <h2 id="ResponseWriter">type <a href="http://localhost:6060/src/net/http/server.go?s=1893:2975#L56">ResponseWriter</a></h2>
  1889. <pre>type ResponseWriter interface {
  1890. <span class="comment">// Header returns the header map that will be sent by</span>
  1891. <span class="comment">// WriteHeader. Changing the header after a call to</span>
  1892. <span class="comment">// WriteHeader (or Write) has no effect unless the modified</span>
  1893. <span class="comment">// headers were declared as trailers by setting the</span>
  1894. <span class="comment">// &#34;Trailer&#34; header before the call to WriteHeader (see example).</span>
  1895. <span class="comment">// To suppress implicit response headers, set their value to nil.</span>
  1896. Header() <a href="index.html#Header">Header</a>
  1897. <span class="comment">// Write writes the data to the connection as part of an HTTP reply.</span>
  1898. <span class="comment">// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)</span>
  1899. <span class="comment">// before writing the data. If the Header does not contain a</span>
  1900. <span class="comment">// Content-Type line, Write adds a Content-Type set to the result of passing</span>
  1901. <span class="comment">// the initial 512 bytes of written data to DetectContentType.</span>
  1902. Write([]<a href="../../builtin/index.html#byte">byte</a>) (<a href="../../builtin/index.html#int">int</a>, <a href="../../builtin/index.html#error">error</a>)
  1903. <span class="comment">// WriteHeader sends an HTTP response header with status code.</span>
  1904. <span class="comment">// If WriteHeader is not called explicitly, the first call to Write</span>
  1905. <span class="comment">// will trigger an implicit WriteHeader(http.StatusOK).</span>
  1906. <span class="comment">// Thus explicit calls to WriteHeader are mainly used to</span>
  1907. <span class="comment">// send error codes.</span>
  1908. WriteHeader(<a href="../../builtin/index.html#int">int</a>)
  1909. }</pre>
  1910. <p>
  1911. A ResponseWriter interface is used by an HTTP handler to
  1912. construct an HTTP response.
  1913. </p>
  1914. <p>
  1915. A ResponseWriter may not be used after the Handler.ServeHTTP method
  1916. has returned.
  1917. </p>
  1918. <div id="example_ResponseWriter_trailers" class="toggle">
  1919. <div class="collapsed">
  1920. <p class="exampleHeading toggleButton"><span class="text">Example (Trailers)</span></p>
  1921. </div>
  1922. <div class="expanded">
  1923. <p class="exampleHeading toggleButton"><span class="text">Example (Trailers)</span></p>
  1924. <p>HTTP Trailers are a set of key/value pairs like headers that come
  1925. after the HTTP response, instead of before.
  1926. </p>
  1927. <p>Code:</p>
  1928. <pre class="code">
  1929. mux := http.NewServeMux()
  1930. mux.HandleFunc(&#34;/sendstrailers&#34;, func(w http.ResponseWriter, req *http.Request) {
  1931. <span class="comment">// Before any call to WriteHeader or Write, declare</span>
  1932. <span class="comment">// the trailers you will set during the HTTP</span>
  1933. <span class="comment">// response. These three headers are actually sent in</span>
  1934. <span class="comment">// the trailer.</span>
  1935. w.Header().Set(&#34;Trailer&#34;, &#34;AtEnd1, AtEnd2&#34;)
  1936. w.Header().Add(&#34;Trailer&#34;, &#34;AtEnd3&#34;)
  1937. w.Header().Set(&#34;Content-Type&#34;, &#34;text/plain; charset=utf-8&#34;) <span class="comment">// normal header</span>
  1938. w.WriteHeader(http.StatusOK)
  1939. w.Header().Set(&#34;AtEnd1&#34;, &#34;value 1&#34;)
  1940. io.WriteString(w, &#34;This HTTP response has both headers before this text and trailers at the end.\n&#34;)
  1941. w.Header().Set(&#34;AtEnd2&#34;, &#34;value 2&#34;)
  1942. w.Header().Set(&#34;AtEnd3&#34;, &#34;value 3&#34;) <span class="comment">// These will appear as trailers.</span>
  1943. })
  1944. </pre>
  1945. </div>
  1946. </div>
  1947. <h2 id="RoundTripper">type <a href="http://localhost:6060/src/net/http/client.go?s=2928:3989#L78">RoundTripper</a></h2>
  1948. <pre>type RoundTripper interface {
  1949. <span class="comment">// RoundTrip executes a single HTTP transaction, returning</span>
  1950. <span class="comment">// a Response for the provided Request.</span>
  1951. <span class="comment">//</span>
  1952. <span class="comment">// RoundTrip should not attempt to interpret the response. In</span>
  1953. <span class="comment">// particular, RoundTrip must return err == nil if it obtained</span>
  1954. <span class="comment">// a response, regardless of the response&#39;s HTTP status code.</span>
  1955. <span class="comment">// A non-nil err should be reserved for failure to obtain a</span>
  1956. <span class="comment">// response. Similarly, RoundTrip should not attempt to</span>
  1957. <span class="comment">// handle higher-level protocol details such as redirects,</span>
  1958. <span class="comment">// authentication, or cookies.</span>
  1959. <span class="comment">//</span>
  1960. <span class="comment">// RoundTrip should not modify the request, except for</span>
  1961. <span class="comment">// consuming and closing the Request&#39;s Body.</span>
  1962. <span class="comment">//</span>
  1963. <span class="comment">// RoundTrip must always close the body, including on errors,</span>
  1964. <span class="comment">// but depending on the implementation may do so in a separate</span>
  1965. <span class="comment">// goroutine even after RoundTrip returns. This means that</span>
  1966. <span class="comment">// callers wanting to reuse the body for subsequent requests</span>
  1967. <span class="comment">// must arrange to wait for the Close call before doing so.</span>
  1968. <span class="comment">//</span>
  1969. <span class="comment">// The Request&#39;s URL and Header fields must be initialized.</span>
  1970. RoundTrip(*<a href="index.html#Request">Request</a>) (*<a href="index.html#Response">Response</a>, <a href="../../builtin/index.html#error">error</a>)
  1971. }</pre>
  1972. <p>
  1973. RoundTripper is an interface representing the ability to execute a
  1974. single HTTP transaction, obtaining the Response for a given Request.
  1975. </p>
  1976. <p>
  1977. A RoundTripper must be safe for concurrent use by multiple
  1978. goroutines.
  1979. </p>
  1980. <pre>var <span id="DefaultTransport">DefaultTransport</span> <a href="index.html#RoundTripper">RoundTripper</a> = &amp;<a href="index.html#Transport">Transport</a>{
  1981. <a href="index.html#Proxy">Proxy</a>: <a href="index.html#ProxyFromEnvironment">ProxyFromEnvironment</a>,
  1982. <a href="index.html#Dial">Dial</a>: (&amp;<a href="../index.html">net</a>.<a href="../index.html#Dialer">Dialer</a>{
  1983. <a href="index.html#Timeout">Timeout</a>: 30 * <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Second">Second</a>,
  1984. <a href="index.html#KeepAlive">KeepAlive</a>: 30 * <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Second">Second</a>,
  1985. }).<a href="index.html#Dial">Dial</a>,
  1986. <a href="index.html#TLSHandshakeTimeout">TLSHandshakeTimeout</a>: 10 * <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Second">Second</a>,
  1987. <a href="index.html#ExpectContinueTimeout">ExpectContinueTimeout</a>: 1 * <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Second">Second</a>,
  1988. }</pre>
  1989. <p>
  1990. DefaultTransport is the default implementation of Transport and is
  1991. used by DefaultClient. It establishes network connections as needed
  1992. and caches them for reuse by subsequent calls. It uses HTTP proxies
  1993. as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and
  1994. $no_proxy) environment variables.
  1995. </p>
  1996. <h3 id="NewFileTransport">func <a href="http://localhost:6060/src/net/http/filetransport.go?s=827:876#L20">NewFileTransport</a></h3>
  1997. <pre>func NewFileTransport(fs <a href="index.html#FileSystem">FileSystem</a>) <a href="index.html#RoundTripper">RoundTripper</a></pre>
  1998. <p>
  1999. NewFileTransport returns a new RoundTripper, serving the provided
  2000. FileSystem. The returned RoundTripper ignores the URL host in its
  2001. incoming requests, as well as most other properties of the
  2002. request.
  2003. </p>
  2004. <p>
  2005. The typical use case for NewFileTransport is to register the &#34;file&#34;
  2006. protocol with a Transport, as in:
  2007. </p>
  2008. <pre>t := &amp;http.Transport{}
  2009. t.RegisterProtocol(&#34;file&#34;, http.NewFileTransport(http.Dir(&#34;/&#34;)))
  2010. c := &amp;http.Client{Transport: t}
  2011. res, err := c.Get(&#34;file:///etc/passwd&#34;)
  2012. ...
  2013. </pre>
  2014. <h2 id="ServeMux">type <a href="http://localhost:6060/src/net/http/server.go?s=53067:53192#L1780">ServeMux</a></h2>
  2015. <pre>type ServeMux struct {
  2016. <span class="comment">// contains filtered or unexported fields</span>
  2017. }</pre>
  2018. <p>
  2019. ServeMux is an HTTP request multiplexer.
  2020. It matches the URL of each incoming request against a list of registered
  2021. patterns and calls the handler for the pattern that
  2022. most closely matches the URL.
  2023. </p>
  2024. <p>
  2025. Patterns name fixed, rooted paths, like &#34;/favicon.ico&#34;,
  2026. or rooted subtrees, like &#34;/images/&#34; (note the trailing slash).
  2027. Longer patterns take precedence over shorter ones, so that
  2028. if there are handlers registered for both &#34;/images/&#34;
  2029. and &#34;/images/thumbnails/&#34;, the latter handler will be
  2030. called for paths beginning &#34;/images/thumbnails/&#34; and the
  2031. former will receive requests for any other paths in the
  2032. &#34;/images/&#34; subtree.
  2033. </p>
  2034. <p>
  2035. Note that since a pattern ending in a slash names a rooted subtree,
  2036. the pattern &#34;/&#34; matches all paths not matched by other registered
  2037. patterns, not just the URL with Path == &#34;/&#34;.
  2038. </p>
  2039. <p>
  2040. If a subtree has been registered and a request is received naming the
  2041. subtree root without its trailing slash, ServeMux redirects that
  2042. request to the subtree root (adding the trailing slash). This behavior can
  2043. be overridden with a separate registration for the path without
  2044. the trailing slash. For example, registering &#34;/images/&#34; causes ServeMux
  2045. to redirect a request for &#34;/images&#34; to &#34;/images/&#34;, unless &#34;/images&#34; has
  2046. been registered separately.
  2047. </p>
  2048. <p>
  2049. Patterns may optionally begin with a host name, restricting matches to
  2050. URLs on that host only. Host-specific patterns take precedence over
  2051. general patterns, so that a handler might register for the two patterns
  2052. &#34;/codesearch&#34; and &#34;codesearch.google.com/&#34; without also taking over
  2053. requests for &#34;<a href="http://www.google.com/">http://www.google.com/</a>&#34;.
  2054. </p>
  2055. <p>
  2056. ServeMux also takes care of sanitizing the URL request path,
  2057. redirecting any request containing . or .. elements or repeated slashes
  2058. to an equivalent, cleaner URL.
  2059. </p>
  2060. <h3 id="NewServeMux">func <a href="http://localhost:6060/src/net/http/server.go?s=53323:53351#L1793">NewServeMux</a></h3>
  2061. <pre>func NewServeMux() *<a href="index.html#ServeMux">ServeMux</a></pre>
  2062. <p>
  2063. NewServeMux allocates and returns a new ServeMux.
  2064. </p>
  2065. <h3 id="ServeMux.Handle">func (*ServeMux) <a href="http://localhost:6060/src/net/http/server.go?s=56368:56428#L1905">Handle</a></h3>
  2066. <pre>func (mux *<a href="index.html#ServeMux">ServeMux</a>) Handle(pattern <a href="../../builtin/index.html#string">string</a>, handler <a href="index.html#Handler">Handler</a>)</pre>
  2067. <p>
  2068. Handle registers the handler for the given pattern.
  2069. If a handler already exists for pattern, Handle panics.
  2070. </p>
  2071. <div id="example_ServeMux_Handle" class="toggle">
  2072. <div class="collapsed">
  2073. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  2074. </div>
  2075. <div class="expanded">
  2076. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  2077. <p>Code:</p>
  2078. <pre class="code">
  2079. mux := http.NewServeMux()
  2080. mux.Handle(&#34;/api/&#34;, apiHandler{})
  2081. mux.HandleFunc(&#34;/&#34;, func(w http.ResponseWriter, req *http.Request) {
  2082. <span class="comment">// The &#34;/&#34; pattern matches everything, so we need to check</span>
  2083. <span class="comment">// that we&#39;re at the root here.</span>
  2084. if req.URL.Path != &#34;/&#34; {
  2085. http.NotFound(w, req)
  2086. return
  2087. }
  2088. fmt.Fprintf(w, &#34;Welcome to the home page!&#34;)
  2089. })
  2090. </pre>
  2091. </div>
  2092. </div>
  2093. <h3 id="ServeMux.HandleFunc">func (*ServeMux) <a href="http://localhost:6060/src/net/http/server.go?s=57531:57618#L1944">HandleFunc</a></h3>
  2094. <pre>func (mux *<a href="index.html#ServeMux">ServeMux</a>) HandleFunc(pattern <a href="../../builtin/index.html#string">string</a>, handler func(<a href="index.html#ResponseWriter">ResponseWriter</a>, *<a href="index.html#Request">Request</a>))</pre>
  2095. <p>
  2096. HandleFunc registers the handler function for the given pattern.
  2097. </p>
  2098. <h3 id="ServeMux.Handler">func (*ServeMux) <a href="http://localhost:6060/src/net/http/server.go?s=55072:55140#L1857">Handler</a></h3>
  2099. <pre>func (mux *<a href="index.html#ServeMux">ServeMux</a>) Handler(r *<a href="index.html#Request">Request</a>) (h <a href="index.html#Handler">Handler</a>, pattern <a href="../../builtin/index.html#string">string</a>)</pre>
  2100. <p>
  2101. Handler returns the handler to use for the given request,
  2102. consulting r.Method, r.Host, and r.URL.Path. It always returns
  2103. a non-nil handler. If the path is not in its canonical form, the
  2104. handler will be an internally-generated handler that redirects
  2105. to the canonical path.
  2106. </p>
  2107. <p>
  2108. Handler also returns the registered pattern that matches the
  2109. request or, in the case of internally-generated redirects,
  2110. the pattern that will match after following the redirect.
  2111. </p>
  2112. <p>
  2113. If there is no registered handler that applies to the request,
  2114. Handler returns a &ldquo;page not found&rdquo; handler and an empty pattern.
  2115. </p>
  2116. <h3 id="ServeMux.ServeHTTP">func (*ServeMux) <a href="http://localhost:6060/src/net/http/server.go?s=56000:56060#L1891">ServeHTTP</a></h3>
  2117. <pre>func (mux *<a href="index.html#ServeMux">ServeMux</a>) ServeHTTP(w <a href="index.html#ResponseWriter">ResponseWriter</a>, r *<a href="index.html#Request">Request</a>)</pre>
  2118. <p>
  2119. ServeHTTP dispatches the request to the handler whose
  2120. pattern most closely matches the request URL.
  2121. </p>
  2122. <h2 id="Server">type <a href="http://localhost:6060/src/net/http/server.go?s=58679:60349#L1971">Server</a></h2>
  2123. <pre>type Server struct {
  2124. Addr <a href="../../builtin/index.html#string">string</a> <span class="comment">// TCP address to listen on, &#34;:http&#34; if empty</span>
  2125. Handler <a href="index.html#Handler">Handler</a> <span class="comment">// handler to invoke, http.DefaultServeMux if nil</span>
  2126. ReadTimeout <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Duration">Duration</a> <span class="comment">// maximum duration before timing out read of the request</span>
  2127. WriteTimeout <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Duration">Duration</a> <span class="comment">// maximum duration before timing out write of the response</span>
  2128. MaxHeaderBytes <a href="../../builtin/index.html#int">int</a> <span class="comment">// maximum size of request headers, DefaultMaxHeaderBytes if 0</span>
  2129. TLSConfig *<a href="../../crypto/tls/index.html">tls</a>.<a href="../../crypto/tls/index.html#Config">Config</a> <span class="comment">// optional TLS config, used by ListenAndServeTLS</span>
  2130. <span class="comment">// TLSNextProto optionally specifies a function to take over</span>
  2131. <span class="comment">// ownership of the provided TLS connection when an NPN</span>
  2132. <span class="comment">// protocol upgrade has occurred. The map key is the protocol</span>
  2133. <span class="comment">// name negotiated. The Handler argument should be used to</span>
  2134. <span class="comment">// handle HTTP requests and will initialize the Request&#39;s TLS</span>
  2135. <span class="comment">// and RemoteAddr if not already set. The connection is</span>
  2136. <span class="comment">// automatically closed when the function returns.</span>
  2137. <span class="comment">// If TLSNextProto is nil, HTTP/2 support is enabled automatically.</span>
  2138. TLSNextProto map[<a href="../../builtin/index.html#string">string</a>]func(*<a href="index.html#Server">Server</a>, *<a href="../../crypto/tls/index.html">tls</a>.<a href="../../crypto/tls/index.html#Conn">Conn</a>, <a href="index.html#Handler">Handler</a>)
  2139. <span class="comment">// ConnState specifies an optional callback function that is</span>
  2140. <span class="comment">// called when a client connection changes state. See the</span>
  2141. <span class="comment">// ConnState type and associated constants for details.</span>
  2142. ConnState func(<a href="../index.html">net</a>.<a href="../index.html#Conn">Conn</a>, <a href="index.html#ConnState">ConnState</a>)
  2143. <span class="comment">// ErrorLog specifies an optional logger for errors accepting</span>
  2144. <span class="comment">// connections and unexpected behavior from handlers.</span>
  2145. <span class="comment">// If nil, logging goes to os.Stderr via the log package&#39;s</span>
  2146. <span class="comment">// standard logger.</span>
  2147. ErrorLog *<a href="../../log/index.html">log</a>.<a href="../../log/index.html#Logger">Logger</a>
  2148. <span class="comment">// contains filtered or unexported fields</span>
  2149. }</pre>
  2150. <p>
  2151. A Server defines parameters for running an HTTP server.
  2152. The zero value for Server is a valid configuration.
  2153. </p>
  2154. <h3 id="Server.ListenAndServe">func (*Server) <a href="http://localhost:6060/src/net/http/server.go?s=62851:62892#L2079">ListenAndServe</a></h3>
  2155. <pre>func (srv *<a href="index.html#Server">Server</a>) ListenAndServe() <a href="../../builtin/index.html#error">error</a></pre>
  2156. <p>
  2157. ListenAndServe listens on the TCP network address srv.Addr and then
  2158. calls Serve to handle requests on incoming connections.
  2159. Accepted connections are configured to enable TCP keep-alives.
  2160. If srv.Addr is blank, &#34;:http&#34; is used.
  2161. ListenAndServe always returns a non-nil error.
  2162. </p>
  2163. <h3 id="Server.ListenAndServeTLS">func (*Server) <a href="http://localhost:6060/src/net/http/server.go?s=67534:67602#L2235">ListenAndServeTLS</a></h3>
  2164. <pre>func (srv *<a href="index.html#Server">Server</a>) ListenAndServeTLS(certFile, keyFile <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#error">error</a></pre>
  2165. <p>
  2166. ListenAndServeTLS listens on the TCP network address srv.Addr and
  2167. then calls Serve to handle requests on incoming TLS connections.
  2168. Accepted connections are configured to enable TCP keep-alives.
  2169. </p>
  2170. <p>
  2171. Filenames containing a certificate and matching private key for the
  2172. server must be provided if neither the Server&#39;s TLSConfig.Certificates
  2173. nor TLSConfig.GetCertificate are populated. If the certificate is
  2174. signed by a certificate authority, the certFile should be the
  2175. concatenation of the server&#39;s certificate, any intermediates, and
  2176. the CA&#39;s certificate.
  2177. </p>
  2178. <p>
  2179. If srv.Addr is blank, &#34;:https&#34; is used.
  2180. </p>
  2181. <p>
  2182. ListenAndServeTLS always returns a non-nil error.
  2183. </p>
  2184. <h3 id="Server.Serve">func (*Server) <a href="http://localhost:6060/src/net/http/server.go?s=63385:63431#L2097">Serve</a></h3>
  2185. <pre>func (srv *<a href="index.html#Server">Server</a>) Serve(l <a href="../index.html">net</a>.<a href="../index.html#Listener">Listener</a>) <a href="../../builtin/index.html#error">error</a></pre>
  2186. <p>
  2187. Serve accepts incoming connections on the Listener l, creating a
  2188. new service goroutine for each. The service goroutines read requests and
  2189. then call srv.Handler to reply to them.
  2190. Serve always returns a non-nil error.
  2191. </p>
  2192. <h3 id="Server.SetKeepAlivesEnabled">func (*Server) <a href="http://localhost:6060/src/net/http/server.go?s=64484:64531#L2139">SetKeepAlivesEnabled</a></h3>
  2193. <pre>func (srv *<a href="index.html#Server">Server</a>) SetKeepAlivesEnabled(v <a href="../../builtin/index.html#bool">bool</a>)</pre>
  2194. <p>
  2195. SetKeepAlivesEnabled controls whether HTTP keep-alives are enabled.
  2196. By default, keep-alives are always enabled. Only very
  2197. resource-constrained environments or servers in the process of
  2198. shutting down should disable them.
  2199. </p>
  2200. <h2 id="Transport">type <a href="http://localhost:6060/src/net/http/transport.go?s=2054:6019#L54">Transport</a></h2>
  2201. <pre>type Transport struct {
  2202. <span class="comment">// Proxy specifies a function to return a proxy for a given</span>
  2203. <span class="comment">// Request. If the function returns a non-nil error, the</span>
  2204. <span class="comment">// request is aborted with the provided error.</span>
  2205. <span class="comment">// If Proxy is nil or returns a nil *URL, no proxy is used.</span>
  2206. Proxy func(*<a href="index.html#Request">Request</a>) (*<a href="../url/index.html">url</a>.<a href="../url/index.html#URL">URL</a>, <a href="../../builtin/index.html#error">error</a>)
  2207. <span class="comment">// Dial specifies the dial function for creating unencrypted</span>
  2208. <span class="comment">// TCP connections.</span>
  2209. <span class="comment">// If Dial is nil, net.Dial is used.</span>
  2210. Dial func(network, addr <a href="../../builtin/index.html#string">string</a>) (<a href="../index.html">net</a>.<a href="../index.html#Conn">Conn</a>, <a href="../../builtin/index.html#error">error</a>)
  2211. <span class="comment">// DialTLS specifies an optional dial function for creating</span>
  2212. <span class="comment">// TLS connections for non-proxied HTTPS requests.</span>
  2213. <span class="comment">//</span>
  2214. <span class="comment">// If DialTLS is nil, Dial and TLSClientConfig are used.</span>
  2215. <span class="comment">//</span>
  2216. <span class="comment">// If DialTLS is set, the Dial hook is not used for HTTPS</span>
  2217. <span class="comment">// requests and the TLSClientConfig and TLSHandshakeTimeout</span>
  2218. <span class="comment">// are ignored. The returned net.Conn is assumed to already be</span>
  2219. <span class="comment">// past the TLS handshake.</span>
  2220. DialTLS func(network, addr <a href="../../builtin/index.html#string">string</a>) (<a href="../index.html">net</a>.<a href="../index.html#Conn">Conn</a>, <a href="../../builtin/index.html#error">error</a>)
  2221. <span class="comment">// TLSClientConfig specifies the TLS configuration to use with</span>
  2222. <span class="comment">// tls.Client. If nil, the default configuration is used.</span>
  2223. TLSClientConfig *<a href="../../crypto/tls/index.html">tls</a>.<a href="../../crypto/tls/index.html#Config">Config</a>
  2224. <span class="comment">// TLSHandshakeTimeout specifies the maximum amount of time waiting to</span>
  2225. <span class="comment">// wait for a TLS handshake. Zero means no timeout.</span>
  2226. TLSHandshakeTimeout <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Duration">Duration</a>
  2227. <span class="comment">// DisableKeepAlives, if true, prevents re-use of TCP connections</span>
  2228. <span class="comment">// between different HTTP requests.</span>
  2229. DisableKeepAlives <a href="../../builtin/index.html#bool">bool</a>
  2230. <span class="comment">// DisableCompression, if true, prevents the Transport from</span>
  2231. <span class="comment">// requesting compression with an &#34;Accept-Encoding: gzip&#34;</span>
  2232. <span class="comment">// request header when the Request contains no existing</span>
  2233. <span class="comment">// Accept-Encoding value. If the Transport requests gzip on</span>
  2234. <span class="comment">// its own and gets a gzipped response, it&#39;s transparently</span>
  2235. <span class="comment">// decoded in the Response.Body. However, if the user</span>
  2236. <span class="comment">// explicitly requested gzip it is not automatically</span>
  2237. <span class="comment">// uncompressed.</span>
  2238. DisableCompression <a href="../../builtin/index.html#bool">bool</a>
  2239. <span class="comment">// MaxIdleConnsPerHost, if non-zero, controls the maximum idle</span>
  2240. <span class="comment">// (keep-alive) to keep per-host. If zero,</span>
  2241. <span class="comment">// DefaultMaxIdleConnsPerHost is used.</span>
  2242. MaxIdleConnsPerHost <a href="../../builtin/index.html#int">int</a>
  2243. <span class="comment">// ResponseHeaderTimeout, if non-zero, specifies the amount of</span>
  2244. <span class="comment">// time to wait for a server&#39;s response headers after fully</span>
  2245. <span class="comment">// writing the request (including its body, if any). This</span>
  2246. <span class="comment">// time does not include the time to read the response body.</span>
  2247. ResponseHeaderTimeout <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Duration">Duration</a>
  2248. <span class="comment">// ExpectContinueTimeout, if non-zero, specifies the amount of</span>
  2249. <span class="comment">// time to wait for a server&#39;s first response headers after fully</span>
  2250. <span class="comment">// writing the request headers if the request has an</span>
  2251. <span class="comment">// &#34;Expect: 100-continue&#34; header. Zero means no timeout.</span>
  2252. <span class="comment">// This time does not include the time to send the request header.</span>
  2253. ExpectContinueTimeout <a href="../../time/index.html">time</a>.<a href="../../time/index.html#Duration">Duration</a>
  2254. <span class="comment">// TLSNextProto specifies how the Transport switches to an</span>
  2255. <span class="comment">// alternate protocol (such as HTTP/2) after a TLS NPN/ALPN</span>
  2256. <span class="comment">// protocol negotiation. If Transport dials an TLS connection</span>
  2257. <span class="comment">// with a non-empty protocol name and TLSNextProto contains a</span>
  2258. <span class="comment">// map entry for that key (such as &#34;h2&#34;), then the func is</span>
  2259. <span class="comment">// called with the request&#39;s authority (such as &#34;example.com&#34;</span>
  2260. <span class="comment">// or &#34;example.com:1234&#34;) and the TLS connection. The function</span>
  2261. <span class="comment">// must return a RoundTripper that then handles the request.</span>
  2262. <span class="comment">// If TLSNextProto is nil, HTTP/2 support is enabled automatically.</span>
  2263. TLSNextProto map[<a href="../../builtin/index.html#string">string</a>]func(authority <a href="../../builtin/index.html#string">string</a>, c *<a href="../../crypto/tls/index.html">tls</a>.<a href="../../crypto/tls/index.html#Conn">Conn</a>) <a href="index.html#RoundTripper">RoundTripper</a>
  2264. <span class="comment">// contains filtered or unexported fields</span>
  2265. }</pre>
  2266. <p>
  2267. Transport is an implementation of RoundTripper that supports HTTP,
  2268. HTTPS, and HTTP proxies (for either HTTP or HTTPS with CONNECT).
  2269. </p>
  2270. <p>
  2271. By default, Transport caches connections for future re-use.
  2272. This may leave many open connections when accessing many hosts.
  2273. This behavior can be managed using Transport&#39;s CloseIdleConnections method
  2274. and the MaxIdleConnsPerHost and DisableKeepAlives fields.
  2275. </p>
  2276. <p>
  2277. Transports should be reused instead of created as needed.
  2278. Transports are safe for concurrent use by multiple goroutines.
  2279. </p>
  2280. <p>
  2281. A Transport is a low-level primitive for making HTTP and HTTPS requests.
  2282. For high-level functionality, such as cookies and redirects, see Client.
  2283. </p>
  2284. <p>
  2285. Transport uses HTTP/1.1 for HTTP URLs and either HTTP/1.1 or HTTP/2
  2286. for HTTPS URLs, depending on whether the server supports HTTP/2.
  2287. See the package docs for more about HTTP/2.
  2288. </p>
  2289. <h3 id="Transport.CancelRequest">func (*Transport) <a href="http://localhost:6060/src/net/http/transport.go?s=14550:14597#L409">CancelRequest</a></h3>
  2290. <pre>func (t *<a href="index.html#Transport">Transport</a>) CancelRequest(req *<a href="index.html#Request">Request</a>)</pre>
  2291. <p>
  2292. CancelRequest cancels an in-flight request by closing its connection.
  2293. CancelRequest should only be called after RoundTrip has returned.
  2294. </p>
  2295. <p>
  2296. Deprecated: Use Request.Cancel instead. CancelRequest can not cancel
  2297. HTTP/2 requests.
  2298. </p>
  2299. <h3 id="Transport.CloseIdleConnections">func (*Transport) <a href="http://localhost:6060/src/net/http/transport.go?s=13939:13981#L386">CloseIdleConnections</a></h3>
  2300. <pre>func (t *<a href="index.html#Transport">Transport</a>) CloseIdleConnections()</pre>
  2301. <p>
  2302. CloseIdleConnections closes any connections which were previously
  2303. connected from previous requests but are now sitting idle in
  2304. a &#34;keep-alive&#34; state. It does not interrupt any connections currently
  2305. in use.
  2306. </p>
  2307. <h3 id="Transport.RegisterProtocol">func (*Transport) <a href="http://localhost:6060/src/net/http/transport.go?s=13408:13476#L370">RegisterProtocol</a></h3>
  2308. <pre>func (t *<a href="index.html#Transport">Transport</a>) RegisterProtocol(scheme <a href="../../builtin/index.html#string">string</a>, rt <a href="index.html#RoundTripper">RoundTripper</a>)</pre>
  2309. <p>
  2310. RegisterProtocol registers a new protocol with scheme.
  2311. The Transport will pass requests using the given scheme to rt.
  2312. It is rt&#39;s responsibility to simulate HTTP request semantics.
  2313. </p>
  2314. <p>
  2315. RegisterProtocol can be used by other packages to provide
  2316. implementations of protocol schemes like &#34;ftp&#34; or &#34;file&#34;.
  2317. </p>
  2318. <p>
  2319. If rt.RoundTrip returns ErrSkipAltProtocol, the Transport will
  2320. handle the RoundTrip itself for that one request, as if the
  2321. protocol were not registered.
  2322. </p>
  2323. <h3 id="Transport.RoundTrip">func (*Transport) <a href="http://localhost:6060/src/net/http/transport.go?s=9257:9319#L253">RoundTrip</a></h3>
  2324. <pre>func (t *<a href="index.html#Transport">Transport</a>) RoundTrip(req *<a href="index.html#Request">Request</a>) (*<a href="index.html#Response">Response</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  2325. <p>
  2326. RoundTrip implements the RoundTripper interface.
  2327. </p>
  2328. <p>
  2329. For higher-level HTTP client support (such as handling of cookies
  2330. and redirects), see Get, Post, and the Client type.
  2331. </p>
  2332. <h2 id="pkg-subdirectories">Subdirectories</h2>
  2333. <div class="pkg-dir">
  2334. <table>
  2335. <tr>
  2336. <th class="pkg-name">Name</th>
  2337. <th class="pkg-synopsis">Synopsis</th>
  2338. </tr>
  2339. <tr>
  2340. <td colspan="2"><a href="../index.html">..</a></td>
  2341. </tr>
  2342. <tr>
  2343. <td class="pkg-name" style="padding-left: 0px;">
  2344. <a href="cgi/index.html">cgi</a>
  2345. </td>
  2346. <td class="pkg-synopsis">
  2347. Package cgi implements CGI (Common Gateway Interface) as specified in RFC 3875.
  2348. </td>
  2349. </tr>
  2350. <tr>
  2351. <td class="pkg-name" style="padding-left: 0px;">
  2352. <a href="cookiejar/index.html">cookiejar</a>
  2353. </td>
  2354. <td class="pkg-synopsis">
  2355. Package cookiejar implements an in-memory RFC 6265-compliant http.CookieJar.
  2356. </td>
  2357. </tr>
  2358. <tr>
  2359. <td class="pkg-name" style="padding-left: 0px;">
  2360. <a href="fcgi/index.html">fcgi</a>
  2361. </td>
  2362. <td class="pkg-synopsis">
  2363. Package fcgi implements the FastCGI protocol.
  2364. </td>
  2365. </tr>
  2366. <tr>
  2367. <td class="pkg-name" style="padding-left: 0px;">
  2368. <a href="httptest/index.html">httptest</a>
  2369. </td>
  2370. <td class="pkg-synopsis">
  2371. Package httptest provides utilities for HTTP testing.
  2372. </td>
  2373. </tr>
  2374. <tr>
  2375. <td class="pkg-name" style="padding-left: 0px;">
  2376. <a href="httputil/index.html">httputil</a>
  2377. </td>
  2378. <td class="pkg-synopsis">
  2379. Package httputil provides HTTP utility functions, complementing the more common ones in the net/http package.
  2380. </td>
  2381. </tr>
  2382. <tr>
  2383. <td class="pkg-name" style="padding-left: 0px;">
  2384. <a href="pprof/index.html">pprof</a>
  2385. </td>
  2386. <td class="pkg-synopsis">
  2387. Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool.
  2388. </td>
  2389. </tr>
  2390. </table>
  2391. </div>
  2392. <div id="footer">
  2393. Build version go1.6.<br>
  2394. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  2395. the content of this page is licensed under the
  2396. Creative Commons Attribution 3.0 License,
  2397. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  2398. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  2399. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  2400. </div>
  2401. </div><!-- .container -->
  2402. </div><!-- #page -->
  2403. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  2404. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  2405. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  2406. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  2407. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  2408. </body>
  2409. </html>