Get the Raw Request String from HttpServletRequest

28,552

Solution 1

Sounds like you need a servlet filter. There is no standard way to handle multipart/form-data, so you'll have to take care to cache this data appropriately when wrapping the HttpServletRequest.

Solution 2

You can read the raw HTTP request by doing:

ServletInputStream in = request.getInputStream();

and then use the regular readmethods of the InputStream.

Hope that helps.

Solution 3

Well, it sounds like you are doing some sort of troubleshooting. Why not just drop the multi-part form component while you are looking at the raw form data. You can use the following JSP snippet to construct the form data.

<%
Enumeration en = request.getParameterNames();
String str = "";
while(en.hasMoreElements()){
   String paramName = (String)en.nextElement();
   String paramValue = request.getParameter(paramName);
   str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue);
}
if (str.length()>0)
   str = str.substring(1);
%>

Solution 4

or if you can write some interceptor to convert the parameter names and values to string format object and set it in request context, or filter is a good idea too.

Solution 5

If you want to get the whole request to a String you can do it with one line of code using the apache IOUtils library.

String myString = org.apache.commons.io.IOUtils.toString(request.getInputStream());
Share:
28,552
Jaime Garcia
Author by

Jaime Garcia

I am a software developer currently working on Java projects for BridgePhase LLC.

Updated on December 02, 2020

Comments

  • Jaime Garcia
    Jaime Garcia over 3 years

    Is it possible to get the raw HTTP Request from the HttpServletRequest object? I want to see the raw request as a String if at all possible.

    I need to get the full text of the request, in this case it's a POST request, so the URL doesn't help. It's also part of a multi-part form, so I can't just call the "getParameterNames()" or "getParameterValues()".

    Thank you,