Source File
doc.go
Belonging Package
net/http
// 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./*Package http provides HTTP client and server implementations.[Get], [Head], [Post], and [PostForm] make HTTP (or HTTPS) requests: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",url.Values{"key": {"Value"}, "id": {"123"}})The caller 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 := io.ReadAll(resp.Body)// ...# Clients and TransportsFor control over HTTP client headers, redirect policy, and othersettings, create a [Client]:client := &http.Client{CheckRedirect: redirectPolicyFunc,}resp, err := client.Get("http://example.com")// ...req, err := http.NewRequest("GET", "http://example.com", nil)// ...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{MaxIdleConns: 10,IdleConnTimeout: 30 * time.Second,DisableCompression: true,}client := &http.Client{Transport: tr}resp, err := client.Get("https://example.com")Clients and Transports are safe for concurrent use by multiplegoroutines and for efficiency should only be created once and re-used.# ServersListenAndServe 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) {fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))})log.Fatal(http.ListenAndServe(":8080", nil))More control over the server's behavior is available by creating acustom Server:s := &http.Server{Addr: ":8080",Handler: myHandler,ReadTimeout: 10 * time.Second,WriteTimeout: 10 * time.Second,MaxHeaderBytes: 1 << 20,}log.Fatal(s.ListenAndServe())# HTTP/2The http package has transparent support for the HTTP/2 protocol.[Server] and [DefaultTransport] automatically enable HTTP/2 supportwhen using HTTPS. [Transport] does not enable HTTP/2 by default.To enable or disable support for HTTP/1, HTTP/2, and/or unencrypted HTTP/2,see the [Server.Protocols] and [Transport.Protocols] configuration fields.To configure advanced HTTP/2 features, see the [Server.HTTP2] and[Transport.HTTP2] configuration fields.Alternatively, the following GODEBUG settings are currently supported:GODEBUG=http2client=0 # disable HTTP/2 client supportGODEBUG=http2server=0 # disable HTTP/2 server supportGODEBUG=http2debug=1 # enable verbose HTTP/2 debug logsGODEBUG=http2debug=2 # ... even more verbose, with frame dumpsThe "omithttp2" build tag may be used to disable the HTTP/2 implementationcontained in the http package.*/package http
![]() |
The pages are generated with Golds v0.8.3-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |