2011-10-24 14:59:31 -06:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
/*
|
2011-10-24 20:29:44 -06:00
|
|
|
Package http provides HTTP client and server implementations.
|
2011-10-24 14:59:31 -06:00
|
|
|
|
2013-10-02 11:21:15 -06:00
|
|
|
Get, Head, Post, and PostForm make HTTP (or HTTPS) requests:
|
2011-10-24 14:59:31 -06:00
|
|
|
|
|
|
|
resp, err := http.Get("http://example.com/")
|
|
|
|
...
|
|
|
|
resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)
|
|
|
|
...
|
|
|
|
resp, err := http.PostForm("http://example.com/form",
|
2012-02-08 20:10:36 -07:00
|
|
|
url.Values{"key": {"Value"}, "id": {"123"}})
|
2011-10-24 14:59:31 -06:00
|
|
|
|
|
|
|
The client must close the response body when finished with it:
|
|
|
|
|
|
|
|
resp, err := http.Get("http://example.com/")
|
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
// ...
|
|
|
|
|
|
|
|
For control over HTTP client headers, redirect policy, and other
|
|
|
|
settings, create a Client:
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
CheckRedirect: redirectPolicyFunc,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Get("http://example.com")
|
|
|
|
// ...
|
|
|
|
|
2012-01-19 07:11:02 -07:00
|
|
|
req, err := http.NewRequest("GET", "http://example.com", nil)
|
|
|
|
// ...
|
2011-10-24 14:59:31 -06:00
|
|
|
req.Header.Add("If-None-Match", `W/"wyzzy"`)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
// ...
|
|
|
|
|
|
|
|
For control over proxies, TLS configuration, keep-alives,
|
|
|
|
compression, and other settings, create a Transport:
|
|
|
|
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{RootCAs: pool},
|
|
|
|
DisableCompression: true,
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
resp, err := client.Get("https://example.com")
|
|
|
|
|
|
|
|
Clients and Transports are safe for concurrent use by multiple
|
|
|
|
goroutines and for efficiency should only be created once and re-used.
|
|
|
|
|
|
|
|
ListenAndServe starts an HTTP server with a given address and handler.
|
|
|
|
The handler is usually nil, which means to use DefaultServeMux.
|
|
|
|
Handle and HandleFunc add handlers to DefaultServeMux:
|
|
|
|
|
|
|
|
http.Handle("/foo", fooHandler)
|
|
|
|
|
|
|
|
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
|
2012-02-10 17:39:57 -07:00
|
|
|
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
|
2011-10-24 14:59:31 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
|
|
|
|
More control over the server's behavior is available by creating a
|
|
|
|
custom Server:
|
|
|
|
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: ":8080",
|
|
|
|
Handler: myHandler,
|
2011-12-12 16:42:56 -07:00
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
2011-10-24 14:59:31 -06:00
|
|
|
MaxHeaderBytes: 1 << 20,
|
|
|
|
}
|
|
|
|
log.Fatal(s.ListenAndServe())
|
2016-01-07 16:43:52 -07:00
|
|
|
|
|
|
|
The http package has transparent support for the HTTP/2 protocol when
|
|
|
|
using HTTPS. Programs that must disable HTTP/2 can do so by setting
|
|
|
|
Transport.TLSNextProto (for clients) or Server.TLSNextProto (for
|
|
|
|
servers) to a non-nil, empty map. Alternatively, the following GODEBUG
|
|
|
|
environment variables are currently supported:
|
|
|
|
|
|
|
|
GODEBUG=http2client=0 # disable HTTP/2 client support
|
|
|
|
GODEBUG=http2server=0 # disable HTTP/2 server support
|
|
|
|
GODEBUG=http2debug=1 # enable verbose HTTP/2 debug logs
|
|
|
|
GODEBUG=http2debug=2 # ... even more verbose, with frame dumps
|
|
|
|
|
|
|
|
The GODEBUG variables are not covered by Go's API compatibility promise.
|
|
|
|
HTTP/2 support was added in Go 1.6. Please report any issues instead of
|
|
|
|
disabling HTTP/2 support: https://golang.org/s/http2bug
|
2011-10-24 14:59:31 -06:00
|
|
|
*/
|
|
|
|
package http
|