Reading HTTP Body is easy. You can use either the following methods to get HTTP Request Body (HTTP Request Payload). Both methods is useful only with HTTP POST message. If you try to read HTTP GET body, you will not get anything (except a null String). This is because HTTP GET message has empty body.
getReader()
This method is defined in Interface ServletRequest. It returns a BufferedReader object. Here is an example on how to read HTTP Request Body using getReader() method.
// inside service(ServletRequest req, ServletResponse res)
// of a class that implements Servlet
// or
// inside doPost(HttpServletRequest req, HttpServletResponse resp)
// of a class that implements HTTPServlet
BufferedReader buff = req.getReader();
char[] buf = new char[4 * 1024]; // 4 KB char buffer
int len;
while ((len = reader.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, len);
}
getInputStream()
This method is defined in Interface ServletRequest. It returns ServletInputStream which is an InputStream object.
// inside service(ServletRequest req, ServletResponse res)
// of a class that implements Servlet
// or
// inside doPost(HttpServletRequest req, HttpServletResponse resp)
// of a class that implements HTTPServlet
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
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;
}
}
}
String body = stringBuilder.toString();
System.out.println(body);
Note that by reading HTTP Request Body by using either of the above method, you cannot read the body again. Also, you can only call getReader() or getInputStream() only once and not both; otherwise you will get IllegalStateException.
If you want to use getReader() or getInputStream() in Filter to intercept, manipulate or reconstruct HTTP Request Body, you must use ServletRequestWrapper to reconstruct or preserve HTTP Request Body. See ServletRequestWrapper example here.
BufferedReader buff = req.getReader();
ReplyDeletechar[] buf = new char[4 * 1024];
int len;
while ((len = reader.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, len);
}
In your code neither the "out" or the "reader" is defined. Did you mean to buff -> reader ? Also what type is out ?
Like your pic dude! Thats our Taj Mahal....:)
ReplyDeleteBtw, thanks for the post...came in real handy!
@bendigi
ReplyDeleteBufferedReader reader = buff;
ServletOutputStream out = res.getOutputStream();
great
ReplyDeleteWhoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteAWS Certification Training Online Toronto
AWS Certification Training Online Newyork
AWS Certification Training Online London
Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
ReplyDeletepython training in chennai
python course institute in chennai
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeletehardware and networking training in chennai
hardware and networking training in tambaram
xamarin training in chennai
xamarin training in tambaram
ios training in chennai
ios training in tambaram
iot training in chennai
iot training in tambaram
Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here
ReplyDeletedata science training in chennai
data science training in omr
android training in chennai
android training in omr
devops training in chennai
devops training in omr
artificial intelligence training in chennai
artificial intelligence training in omr
Great Blog to read, It gives more useful information. Thank lot.
ReplyDeletePython Training In Bangalore
Python Training Institute In Bangalore
informative article .thanks for sharing.Angular training in Chennai
ReplyDeleteThanks for this amazing blog , it is very useful content for us
ReplyDeletekeep sharing this type of informtion if anyone is looking for the best training institute in nodia visit us.
Python Training Institute
data science training in noida
machine learning institute in noida
java training institute in noida
data science training in noida
Thanks for this amazing blog , it is very useful content for us
ReplyDeletekeep sharing this type of informtion if anyone is looking for the best digital marketing training institute in ghaziabad visit us.
https://theprimeinstitute.in/
NICE ARTICLE
ReplyDeleteinformation technology