Issue
I am trying to detect if an http request has body without reading it or doing anything that makes it impossible for the existing code to do with it whatever it does later in the flow.
boolean hasBody(HttpServletRequest http) {
boolean hasBody = false;
try {
hasBody = http.getInputStream() != null; // always gives true
hasBody = http.getReader().ready(); // always gives IllegalStateException
} catch (IOException e) {
}
return hasBody;
}
Unfortunately both checks I could come up with don't work when I test them both as GET and as POST with body.
Note, I don't want to do: "POST".equals(http.getMethod())
or !"GET".equals(http.getMethod())
if possible, 'cause I'm not sure what methods there could be with or without body.
Solution
You can use getContentLength() or getContentLengthLong() and check if it is positive.
Answered By - Andrew Sokolov
Answer Checked By - Marilyn (JavaFixing Volunteer)