Standard library / net/http
http.ErrBodyNotAllowed
Returned for attempts to write a body when the HTTP status disallows it. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
var ErrBodyNotAllowed errorWorking Example
Example Snippetgo
package main
import ( "errors" "fmt" "net/http" "net/http/httptest")
func main() { ch := make(chan bool, 1) s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) _, e := w.Write([]byte("x")) ch <- errors.Is(e, http.ErrBodyNotAllowed) })) defer s.Close() res := must(s.Client().Get(s.URL)) check(res.Body.Close()) fmt.Println(<-ch)}
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) }}