Standard library / net/http
http.Response
Represent an HTTP response; callers of Client.Do must close the returned body. Expected stdout: '200 yes\n'.
Formal Syntax & Structure
go syntaxgo
type Response struct { Status string StatusCode int Proto string ProtoMajor int ProtoMinor int
Header Header
Body io.ReadCloser
ContentLength int64
TransferEncoding []string
Close bool
Uncompressed bool
Trailer Header
Request *Request
TLS *tls.ConnectionState}Working Example
Example Snippetgo
package main
import ( "fmt" "io" "net/http" "net/http/httptest")
func main() { s := fixture() defer s.Close() r := must(s.Client().Get(s.URL)) defer r.Body.Close() fmt.Println(r.StatusCode, r.Header.Get("X-Demo"))}func fixture() *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-Demo", "yes") if r.Method == "POST" { b, e := io.ReadAll(io.LimitReader(r.Body, 1025)) check(e) if len(b) > 1024 { w.WriteHeader(413) return } fmt.Fprint(w, string(b)) return } fmt.Fprint(w, "hello") }))}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}