0

I am using Filter to intercept each request, where in filter I can get only ServletRequest inputstream, which is returning empty input stream, to get the input stream.It seems that I need to get the HttpServletRequest. How to achieve this? I tried casting as well as converting to httpservletrequest. none of the ways, i was able to get value using

request.getParameter("aaa") //when request content type is multipart/file.
dur
  • 15,689
  • 25
  • 79
  • 125
adarsha bv
  • 43
  • 2
  • 13

3 Answers3

1

getInputStream() is a part of ServletRequest interface, so even if you cast ServletRequest to HttpServletRequest ( which extends from ServletRequest) the result will be the same.

Perhaps this answer will help you to find out why inputstream is empty.

Community
  • 1
  • 1
eparvan
  • 1,639
  • 1
  • 15
  • 26
1

If the input stream is empty there was no request body. Getting the HttpServletRequest won't change that: in fact, you already have it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • If the input stream is empty but the parameters aren't, the parameters are available via, err, `request.getParameter()` and the input stream is, err, empty. – user207421 Nov 07 '16 at 09:03
0

I was successfully able to type cast the "servletRequest" into "HttpServletRequest" inside my custom filter class. Below is the snippet,

public class SAMLFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;

        if(servletRequest instanceof HttpServletRequest) {
            httpServletRequest = (HttpServletRequest)servletRequest;
            System.out.println("getInputStream = " + httpServletRequest.getInputStream());

        } else {
            System.out.println("NOT CAST");
        }


    @Override
    public void destroy() {

    }
}
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Kam1515
  • 1
  • 1