github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/net/http/doc.go (about) 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 /* 6 Package http は HTTP クライアントとサーバーの実装を提供します。 7 8 [Get]、[Head]、[Post]、[PostForm] は HTTP (または HTTPS) リクエストを行います: 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", 15 url.Values{"key": {"Value"}, "id": {"123"}}) 16 17 関数を呼び出した後、レスポンスボディを閉じる必要があります。 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 := io.ReadAll(resp.Body) 25 // ... 26 27 # Clients and Transports 28 29 HTTP クライアントヘッダー、リダイレクトポリシー、その他の設定を制御するには、[Client] を作成してください。 30 31 client := &http.Client{ 32 CheckRedirect: redirectPolicyFunc, 33 } 34 35 resp, err := client.Get("http://example.com") 36 // ... 37 38 req, err := http.NewRequest("GET", "http://example.com", nil) 39 // ... 40 req.Header.Add("If-None-Match", `W/"wyzzy"`) 41 resp, err := client.Do(req) 42 // ... 43 44 プロキシ、TLS 設定、Keep-Alive、圧縮、その他の設定を制御するには、[Transport] を作成してください。 45 46 tr := &http.Transport{ 47 MaxIdleConns: 10, 48 IdleConnTimeout: 30 * time.Second, 49 DisableCompression: true, 50 } 51 client := &http.Client{Transport: tr} 52 resp, err := client.Get("https://example.com") 53 54 クライアントとトランスポートは、複数のゴルーチンによる同時使用に対して安全であり、効率的に使用するためには、1度だけ作成して再利用する必要があります。 55 56 # Servers 57 58 ListenAndServe は、指定されたアドレスとハンドラーで HTTP サーバーを開始します。 59 ハンドラーは通常 nil で、[DefaultServeMux] を使用することを意味します。 60 [Handle] と [HandleFunc] は、[DefaultServeMux] にハンドラーを追加します。 61 62 http.Handle("/foo", fooHandler) 63 64 http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { 65 fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) 66 }) 67 68 log.Fatal(http.ListenAndServe(":8080", nil)) 69 70 サーバーの動作に関するより詳細な制御は、カスタムサーバーを作成することで利用できます。 71 72 s := &http.Server{ 73 Addr: ":8080", 74 Handler: myHandler, 75 ReadTimeout: 10 * time.Second, 76 WriteTimeout: 10 * time.Second, 77 MaxHeaderBytes: 1 << 20, 78 } 79 log.Fatal(s.ListenAndServe()) 80 81 # HTTP/2 82 83 Go 1.6 以降、HTTPS を使用する場合、http パッケージは HTTP/2 プロトコルの透過的なサポートを提供します。HTTP/2 を無効にする必要があるプログラムは、[Transport.TLSNextProto] (クライアント用) または [Server.TLSNextProto] (サーバー用) を nil でない空のマップに設定することで行えます。また、次の GODEBUG 設定が現在サポートされています。 84 85 GODEBUG=http2client=0 # HTTP/2 クライアントサポートを無効にする 86 GODEBUG=http2server=0 # HTTP/2 サーバーサポートを無効にする 87 GODEBUG=http2debug=1 # 詳細な HTTP/2 デバッグログを有効にする 88 GODEBUG=http2debug=2 # ... フレームダンプを含めて、より詳細なログを有効にする 89 90 HTTP/2 サポートを無効にする前に、問題がある場合は報告してください: https://golang.org/s/http2bug 91 92 http パッケージの [Transport] と [Server] は、単純な構成に対して自動的に HTTP/2 サポートを有効にします。より複雑な構成で HTTP/2 を有効にする、より低レベルの HTTP/2 機能を使用する、またはより新しいバージョンの Go の http2 パッケージを使用するには、直接 "golang.org/x/net/http2" をインポートし、その ConfigureTransport および/または ConfigureServer 関数を使用します。golang.org/x/net/http2 パッケージを使用して HTTP/2 を手動で設定する場合、net/http パッケージの組み込みの HTTP/2 サポートよりも優先されます。 93 */ 94 package http