0

I have a Java servlet and when I send it a POST request everything works just fine. The thing is that if send a GET request i want to make the same procedure as if it is a POST req, when I run request.getParameterMap() has size = 0.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response); 
    }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LinkedList<String> lErr = new LinkedList<String>();

        System.out.printf("Request size: %d%n", request.getContentLength());
        System.out.printf("Request method: %s%n", request.getMethod());

        Map map = request.getParameterMap();

        System.out.printf("ParameterMap size: %d%n", map.size());
}

What could the problem be?

Both POST and GET request have data in their body. I am sending it using form-data as getParameterMap() does not only supports query string but that also. POST request works just fine.

  • From the information you've given, I'd say that you aren't providing any parameters in the URL query string. – tgdavies Sep 07 '21 at 07:47
  • I am currently sending data via x-www-form-urlencoded so it goes into the body. I have read that getParameterMap does not only work in the query string but also in the form data. I have revised both requests (GET and POST) and the only difference is that the method is different. – Carlos Martel Lamas Sep 07 '21 at 07:50
  • 2
    Please read https://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get – tgdavies Sep 07 '21 at 07:52
  • I got some info from the post you just told me to read. getParameterMap() behaves differently when called with a GET request OR POST? @tgdavies – Carlos Martel Lamas Sep 07 '21 at 08:16
  • 2
    "The thing is that if send a GET request i want to make the same procedure as if it is a POST req". This is fundamentally wrong. – João Dias Sep 07 '21 at 09:30
  • 2
    GET requests don't have bodies. The parameters come from the query string – tgdavies Sep 07 '21 at 09:39
  • 1
    The parameters in a GET request come from the URL query portion, not from the body of the request, which is ignored. – user207421 Sep 07 '21 at 09:43

1 Answers1

0

As Joao and some more people pointed out, the way I was told to do this stuff was wrong. GET request get their body ignored therefore parametermap values come from the querystring not the body. So if nothing goes via querystring but in form-data in a GET request, parametermap size will be 0. Thank you guys!

The thing will to start using the doGet() method properly and not just passing that parameters to doPost().

I was so focused on the task that i couldn't even think properly.