Set HTTP Request Body Size in Go

Use http.MaxBytesReader to limit and control the size HTTP request body. This is a good practice to prevent abuse and save bandwidth. In the example below, I set a 20 bytes HTTP request body size limit. package main import ( "" "io" "log" "net/http" "time" ) func main() { router := httprouter.New() router.HandlerFunc(http.MethodPost, "/", func(w http.ResponseWriter, r *http.Request) { buffer, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 20)) defer r.Body.Close() if err != nil { w....

October 23, 2023 ยท John Pili