I want to allow uploading files. Go is being used server-side to handle requests. I would like to send a response \"File too large\" whenever the file they\'re trying to upl
Most clients do not read the response until done writing the request. Responding with an error from the server will not cause these clients to stop writing.
The net/http server supports the 100 continue status. To use this feature, the server application should respond with an error before reading the request body:
func handler(w http.ResponseWriter, r *http.Request) {
if r.ContentLength > MaxFileSize {
http.Error(w, "request too large", http.StatusExpectationFailed)
return
}
r.Body = http.MaxBytesReader(w, r.Body, MaxFileSize)
err := r.ParseMultipartForm(1024)
// ... continue as before
If the client sent the "Expect: 100-continue" header, then the client should wait for the 100 continue status before writing the request body. The net/http server automatically sends the 100 continue status when the server application reads the request body. The server can stop the client from writing the request body by replying with an error before reading the request.
The net/http client does not support the 100 continue status.
If the client did not send the expect header and the server application returns from the request handler without reading the complete request body, then the net/http server reads and discards up to 256 << 10 bytes of the request body. The server will closes the connection if the entire request body was not read.