Jaime Garcia
Jaime Garcia

Reputation: 7096

Get the Raw Request String from HttpServletRequest

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,

Upvotes: 19

Views: 31500

Answers (5)

PbxMan
PbxMan

Reputation: 7625

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());

Upvotes: 0

monchote
monchote

Reputation: 129

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.

Upvotes: 12

Chris J
Chris J

Reputation: 9252

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);
%>

Upvotes: 4

Puran
Puran

Reputation: 994

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.

Upvotes: 1

McDowell
McDowell

Reputation: 108859

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.

Upvotes: 0

Related Questions