• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading an empty request body  RSS feed

 
Shreya De
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Can someone please tell me what happens when we try to read an empty POST request body? The server code that tries to read the request body is something like this =>
BufferedReader reader = context.getRequest().getReader(); // context here is an instance of javax.servlet.jsp.PageContext which has been set already for the current page.

Will the reader be set to null in this case when the request body is empty ? My server sends an error response , which can be caused by a null pointer exception. However, there are no logs to verify this .Please help.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I recall, your first attempt to read will behave like you reached the end of the stream.

What happened when you tried it?

Why are you using context.getRequest() - ? in a doGet or doPost method you should already be handed an HttpServletRequest instance.

Bill
 
Shreya De
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill.
What is handed over to me is the the PageContext instance and I am able to get the request headers such as content-type ,content-length properly using calls such as context.getRequest().getContentType().
Hence , I am able to fetch the HttpServletRequest instance properly.

<What happened when you tried it? >
After I do context.getRequest().getReader() , I run a for loop to read the request body and append it to a stringbuffer . Something like this :

BufferedReader reader = context.getRequest().getReader();
for( String s = reader.readLine(); s != null; s = reader.readLine() )
{
_request.append( s );
}

If reader is null ,reader.readLine() would throw a null pointer exception , which will eventually lead to the error response I am seeing .
However , when I tested by creating a request with body envelope set to {} , no exception was thrown.The issue was not reproduced. The buffer just stored an empty string. Hence , the doubt.

<your first attempt to read will behave like you reached the end of the stream.>
Will this return null ? If not , in what condition can the request.getReader() be null ?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If reader is null ,reader.readLine() would throw a null pointer exception , which will eventually lead to the error response I am seeing .


Exactly what exception message are you seeing?



Will throw a NPE if context is null OR if getRequest() returns null. If this was my problem I would be checking for both. In addition, if the input stream has already be acquired by other code, getReader() will throw an IllegalStateException.

What is handed over to me is the the PageContext instance

Somebody made a bad architectural decision upstream from you. (in my not very humble opinion)

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shreya De wrote:What is handed over to me is the the PageContext instance

In a servlet?

Good grief, what a horrible decision!
 
Shreya De
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my case , context.getRequest() was not null . Hence I was suspecting the getReader() call to return null .
But I was wrong . I was finally able to reproduce the issue and found that getReader() returned an empty buffer ( readline () did not throw NPE).
The root cause of NPE was a JSON exception thrown while trying to parse this empty request . Thanks for your inputs .
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the follow-up on the real cause of your problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!