Reputation: 9827
I have a Filter that receives the HttpServletRequest and the request is a POST that consists of an xml that I need to read into my filter method. What is the best way to get the posted xml from the HttpServletRequest object.
Upvotes: 9
Views: 13892
Reputation: 1109062
That depends on how the client has sent it.
If it's been sent as the raw request body, then use ServletRequest#getInputStream()
:
InputStream xml = request.getInputStream();
// ...
If it's been sent as a regular application/x-www-form-urlencoded
request parameter, then use ServletRequest#getParameter()
:
String xml = request.getParameter("somename");
// ...
If it's been sent as an uploaded file in flavor of a multipart/form-data
part, then use HttpServletRequest#getPart()
.
InputStream xml = request.getPart("somename").getInputStream();
// ...
That were the ways supported by the standard servlet API. Other ways may require a different or 3rd party API (e.g. SOAP).
Upvotes: 6