I am trying to get the contents of an HttpServletRequest. Here is how I do it:
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
String content = "";
try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
content = stringBuilder.toString();
System.out.println("Length: " + request.getContentLength());
The string "content" is empty. However, the last line displays
Length: 53
which is exactly the length of the content that I expect. If this helps, here is how I run this servlet:
wget --post-data='{"imei":"351553012623446","hni":"310150","wdp":false}' http://localhost:8080/test/forward
source
share