Servlet getParameter() returns null for form-data

3.2k views Asked by At

I am sending a POST request to my localhost Tomcat 8.0 Servlet 3.1 web application but request.getParameter("") returns me null.

This is how I make my request. Using PostMan

I am using PostMan to perform my POST request. And on the java side, I am calling request.getParameter("code") and this gives me null. Same goes for qwe field. I am not using any framework. It's raw servlets. This is only a back-end so it's all about handling the data and responding to client.

If I use "x-www-form-urlencoded", I am able to retrieve the parameters through getParameter() call but I still want to know why I am not able to get form-data.

Thanks.

1

There are 1 answers

2
OmerHalit On BEST ANSWER

Thanks to @cjstehno,

When he said "form-data" was actually a multipart data, then I attempted to read it as multipart data but taking isFormField() method in mind to distinguish between file and parameter. So from a raw servlet, one can read form-data through the code below. From the performance view, I am pretty sure that this might be improved.

try {
     ServletFileUpload upload = new ServletFileUpload();
     FileItemIterator iter = upload.getItemIterator(request);

     while (iter.hasNext()) {
          FileItemStream item = iter.next();
          String name = item.getFieldName();

          if (item.isFormField()) {
               String value = Streams.asString(item.openStream());
          }
     }          
 } catch (Exception ex) {}