Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop processing a http POST request if it is larger than x size

Tags:

go

If I have a basic http handler for POST requests, how can I stop processing if the payload is larger than 100 KB?

From what I understand, in my POST Handler, behind the scenes the server is streaming the POSTED data. But if I try and access it, it will block correct? I want to stop processing if it is over 100 KB in size.

like image 731
cool breeze Avatar asked Nov 17 '15 20:11

cool breeze


People also ask

What is the maximum size of HTTP POST request?

The default value of the HTTP and HTTPS connector maximum post size is 2MB. However you can adjust the value as per your requirement. The below command to set the connector to accept maximum 100,000 bytes. If the http request POST size exceeds the 100,000 bytes then connector return HTTP/1.1 400 Bad Request.

What HTTP method has no restrictions on data length?

Data passed using the POST method will not visible in query parameters in browser URL. parameters of POST methods are not saved in browser history. There is no restriction in sending the length of data. It helps you to securely pass sensitive and confidential information like login details to server.

How big can an HTTP response be?

The response limit is 9,223,372,036,854,775,807 bytes (2 ** 64).

What is the limit for Get URL and POST payload?

Internet Explorer also has a maximum path length of 2,048 characters. This limit applies to both POST request and GET request URLs. If you are using the GET method, you are limited to a maximum of 2,048 characters, minus the number of characters in the actual path.


1 Answers

Use http.MaxBytesReader to limit the amount of data read from the client. Execute this line of code

r.Body = http.MaxBytesReader(w, r.Body, 100000) 

before calling r.ParseForm, r.FormValue or any other request method that reads the body.

Wrapping the request body with io.LimitedReader limits the amount of data read by the application, but does not necessarily limit the amount of data read by the server on behalf of the application.

Checking the request content length is unreliable because the field is not set to the actual request body size when chunked encoding is used.

like image 102
Bayta Darell Avatar answered Nov 15 '22 09:11

Bayta Darell