Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve raw post data from HttpServletRequest in java

I'm trying to get the post data in Java. Seems like it should be one of the simplest things to do right? I mean, HttpServletRequest.getParameter has to do it right? So how can you get the raw post data?

I found HttpServletRequest get JSON POST data and used Kdeveloper's code to pull the post data from a request. It works, but theres a catch: I can only get that post data once.

Heres the method I made from Kdeveloper's code:

public static String getPostData(HttpServletRequest req) {     StringBuilder sb = new StringBuilder();     try {         BufferedReader reader = req.getReader();         reader.mark(10000);          String line;         do {             line = reader.readLine();             sb.append(line).append("\n");         } while (line != null);         reader.reset();         // do NOT close the reader here, or you won't be able to get the post data twice     } catch(IOException e) {         logger.warn("getPostData couldn't.. get the post data", e);  // This has happened if the request's reader is closed         }      return sb.toString(); } 

Previously I had closed the reader at the end of this method, but that caused exceptions when the method ran more than once on the same request. Without closing it, no exceptions happen, but the method returns an empty string.

Honestly, there should just be an exposed req.getPostData() method - did no one think that would be useful?

So how can I write this method such that it always returns the correct post data?

like image 572
B T Avatar asked Feb 17 '11 00:02

B T


People also ask

Can we get request body from HttpServletRequest?

HttpServletRequest and Request Body So when we're dealing with the HTTP requests, HttpServletRequest provides us two ways to read the request body - getInputStream and getReader methods.

What is the difference between ServletRequest and HttpServletRequest?

ServletRequest provides basic setter and getter methods for requesting a Servlet, but it doesn't specify how to communicate. HttpServletRequest extends the Interface with getters for HTTP-communication (which is of course the most common way for communicating since Servlets mostly generate HTML).

What is the use of HttpServletRequestWrapper?

Class HttpServletRequestWrapper. Provides a convenient implementation of the HttpServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object.

What is HttpServletRequest spring boot?

Spring Boot HttpServletRequest Logging Filter. A Request Logging Filter for Spring Boot applications, that allows you to read the request body multiple times. The request body is cached using an overridden HttpServletRequestWrapper. Then a reader that allows the client to read the body multiple times is returned.


1 Answers

The request body is available as byte stream by HttpServletRequest#getInputStream():

InputStream body = request.getInputStream(); // ... 

Or as character stream by HttpServletRequest#getReader():

Reader body = request.getReader(); // ... 

Note that you can read it only once. The client ain't going to resend the same request multiple times. Calling getParameter() and so on will implicitly also read it. If you need to break down parameters later on, you've got to store the body somewhere and process yourself.

like image 190
BalusC Avatar answered Sep 17 '22 12:09

BalusC