So I am self-taught and green on handling requests, but it seems to me that in the case of no request body, the BufferedReader
should output null if I call .readLine()
again after running this kind of while loop:
while (!(line = br.readLine()).isBlank()){ some code here }
So I have done this to successfully load up method and headers into variables, and everything is working swell until I decided to add a bit of code to detect (not yet handle) the presence of a request body
try {
if ((body = br.readLine()) == null) {
System.out.println("body = " + body);
reqLog.append("- Request body: falsen");
} else {
reqLog.append("- Request body: truen");
}
} catch (IOException e){
e.printStackTrace();
}
The code above blocks my thread – my web browser patiently waits while nothing visually happens on my server. Everything within this block executes however when I cancel the browser connection. Even the System.out.println(body = null)
then pops up in the terminal, before the error for an aborted connection takes over.
Removing the try-block above solves everything.
I am super grateful for a pointer toward proper detection of a request body as well!
T-Stone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6