Package net/http

The http package provides HTTP client and server implementations. It handles details such as connection pooling, HTTP/2 support, and automatic decompression.

The package is built around two main interfaces: Client for making HTTP requests and Server for listening and responding to HTTP requests.

Constants

  • DefaultMaxHeaderBytes DefaultMaxHeaderBytes = 1 << 20
    DefaultMaxHeaderBytes is the maximum difference between the number of bytes a client may send and the number of bytes a server may admit in an HTTP request header. This is a security measure to prevent clients from sending excessively large headers and potentially exhausting server memory.
  • StatusContinue StatusContinue = 100
    StatusContinue is the HTTP status code for Continue.
  • StatusInternalServerError StatusInternalServerError = 500
    StatusInternalServerError is the HTTP status code for Internal Server Error.

Variables

  • DefaultClient var DefaultClient *Client = &Client{}
    DefaultClient is the default Client for making HTTP requests. It is safe for concurrent use by multiple goroutines.
  • DefaultServeMux var DefaultServeMux *ServeMux = NewServeMux()
    DefaultServeMux is the default ServeMux used by the ListenAndServe function. It is safe for concurrent use by multiple goroutines.

Types

  • Client type Client struct { ... }
    A Client represents an HTTP client. The zero value for Client is ready to use. It is safe for concurrent use by multiple goroutines.
  • Request type Request struct { ... }
    A Request represents an HTTP request. Client methods (like Get, Post, etc.) create Request values that are suitable for use with that method.
  • Response type Response struct { ... }
    A Response represents an HTTP response.
  • ServeMux type ServeMux struct { ... }
    A ServeMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and invokes the handler for the pattern that most closely matches the URL.

Functions

  • ListenAndServe func ListenAndServe(addr string, handler Handler) error
    ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. If the handler is nil, DefaultServeMux is used. ListenAndServe always returns a non-nil error.
    
    package main
    
    import (
        "fmt"
        "log"
        "net/http"
    )
    
    func main() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, "Hello, %s!", r.URL.Path)
        })
    
        fmt.Println("Starting server on :8080")
        err := http.ListenAndServe(":8080", nil)
        if err != nil {
            log.Fatal("ListenAndServe: ", err)
        }
    }
                                    
  • Get func Get(url string) (resp *Response, err error)
    Get sends an HTTP GET request to the specified URL. It is a shorthand for DefaultClient.Get. The response body is not closed automatically.
  • Post func Post(url string, contentType string, body io.Reader) (resp *Response, err error)
    Post sends an HTTP POST request to the specified URL. It is a shorthand for DefaultClient.Post. The response body is not closed automatically.

Client Functions

  • Client.Do func (c *Client) Do(req *Request) (*Response, error)
    Do issues an HTTP request and returns the response. The transport used by the client is selected based on the presence of fields like Transport, Dial, or DialTLS.
  • Client.Get func (c *Client) Get(url string) (resp *Response, err error)
    Get sends an HTTP GET request to the specified URL.
  • Client.Post func (c *Client) Post(url string, contentType string, body io.Reader) (resp *Response, err error)
    Post sends an HTTP POST request to the specified URL.

Server Functions

  • Server.ServeHTTP func (srv *Server) ServeHTTP(listener net.Listener) error
    ServeHTTP serves HTTP requests on the listener. It is a blocking call and will not return until the server is stopped.
  • Server.Shutdown func (srv *Server) Shutdown(ctx context.Context) error
    Shutdown gracefully shuts down the server without interrupting any active ServeHTTP calls.