I am trying to implement a restriction on file size when uploading files. The following code correctly detects whenever the file (or rather the request body, but close enough for my purposes) is larger than 1 MB. If this is the case however, the page returned is a "The connection was reset" page, instead of a page with my custom error message. If the file is under 1 MB I correctly get a page saying "File upload OK".
I would love some pointers on why the connection to the server is reset instead of serving what I specify in the code below.
func baseHandler(writer http.ResponseWriter, request *http.Request) {
request.Body = http.MaxBytesReader(writer, request.Body, 1024 * 1024)
_, _, err := request.FormFile("uploadfile")
if err != nil {
fmt.Fprintf(writer, "ERROR: %v", err) // Should be displayed in browser, is not
fmt.Printf("ERROR: %v", err) // Gets written to terminal, so any errors are correctly identified
return
}
fmt.Fprintf(writer, "File upload OK")
}
func main() {
http.HandleFunc("/", baseHandler)
http.ListenAndServe(":8080", nil)
}