Brad Fitzpatrick | 2b5aa28 | 2011-10-24 13:59:31 -0700 | [diff] [blame] | 1 | // Copyright 2011 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | /* |
Brad Fitzpatrick | d72267a | 2011-10-24 19:29:44 -0700 | [diff] [blame] | 6 | Package http provides HTTP client and server implementations. |
Brad Fitzpatrick | 2b5aa28 | 2011-10-24 13:59:31 -0700 | [diff] [blame] | 7 | |
Russ Cox | 1b0bffa | 2013-10-02 13:21:15 -0400 | [diff] [blame] | 8 | Get, Head, Post, and PostForm make HTTP (or HTTPS) requests: |
Brad Fitzpatrick | 2b5aa28 | 2011-10-24 13:59:31 -0700 | [diff] [blame] | 9 | |
| 10 | resp, err := http.Get("http://example.com/") |
| 11 | ... |
| 12 | resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf) |
| 13 | ... |
| 14 | resp, err := http.PostForm("http://example.com/form", |
Brad Fitzpatrick | 3484d54 | 2012-02-09 14:10:36 +1100 | [diff] [blame] | 15 | url.Values{"key": {"Value"}, "id": {"123"}}) |
Brad Fitzpatrick | 2b5aa28 | 2011-10-24 13:59:31 -0700 | [diff] [blame] | 16 | |
| 17 | The client must close the response body when finished with it: |
| 18 | |
| 19 | resp, err := http.Get("http://example.com/") |
| 20 | if err != nil { |
| 21 | // handle error |
| 22 | } |
| 23 | defer resp.Body.Close() |
| 24 | body, err := ioutil.ReadAll(resp.Body) |
| 25 | // ... |
| 26 | |
| 27 | For control over HTTP client headers, redirect policy, and other |
| 28 | settings, create a Client: |
| 29 | |
| 30 | client := &http.Client{ |
| 31 | CheckRedirect: redirectPolicyFunc, |
| 32 | } |
| 33 | |
| 34 | resp, err := client.Get("http://example.com") |
| 35 | // ... |
| 36 | |
Christoph Hack | 2a6b4e1 | 2012-01-19 06:11:02 -0800 | [diff] [blame] | 37 | req, err := http.NewRequest("GET", "http://example.com", nil) |
| 38 | // ... |
Brad Fitzpatrick | 2b5aa28 | 2011-10-24 13:59:31 -0700 | [diff] [blame] | 39 | req.Header.Add("If-None-Match", `W/"wyzzy"`) |
| 40 | resp, err := client.Do(req) |
| 41 | // ... |
| 42 | |
| 43 | For control over proxies, TLS configuration, keep-alives, |
| 44 | compression, and other settings, create a Transport: |
| 45 | |
| 46 | tr := &http.Transport{ |
| 47 | TLSClientConfig: &tls.Config{RootCAs: pool}, |
| 48 | DisableCompression: true, |
| 49 | } |
| 50 | client := &http.Client{Transport: tr} |
| 51 | resp, err := client.Get("https://example.com") |
| 52 | |
| 53 | Clients and Transports are safe for concurrent use by multiple |
| 54 | goroutines and for efficiency should only be created once and re-used. |
| 55 | |
| 56 | ListenAndServe starts an HTTP server with a given address and handler. |
| 57 | The handler is usually nil, which means to use DefaultServeMux. |
| 58 | Handle and HandleFunc add handlers to DefaultServeMux: |
| 59 | |
| 60 | http.Handle("/foo", fooHandler) |
| 61 | |
| 62 | http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { |
Bjorn Tipling | 0846e27 | 2012-02-10 22:39:57 -0200 | [diff] [blame] | 63 | fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) |
Brad Fitzpatrick | 2b5aa28 | 2011-10-24 13:59:31 -0700 | [diff] [blame] | 64 | }) |
| 65 | |
| 66 | log.Fatal(http.ListenAndServe(":8080", nil)) |
| 67 | |
| 68 | More control over the server's behavior is available by creating a |
| 69 | custom Server: |
| 70 | |
| 71 | s := &http.Server{ |
| 72 | Addr: ":8080", |
| 73 | Handler: myHandler, |
David Symonds | 3dbecd5 | 2011-12-13 10:42:56 +1100 | [diff] [blame] | 74 | ReadTimeout: 10 * time.Second, |
| 75 | WriteTimeout: 10 * time.Second, |
Brad Fitzpatrick | 2b5aa28 | 2011-10-24 13:59:31 -0700 | [diff] [blame] | 76 | MaxHeaderBytes: 1 << 20, |
| 77 | } |
| 78 | log.Fatal(s.ListenAndServe()) |
| 79 | */ |
| 80 | package http |