3

I am doing a simple Java Servlet POST request without using any HTML and only using Postman. And the response from getParameter() is always null.

Here is the servlet:

@WebServlet("/api/form")
public class FormServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String orderNumber = req.getParameter("testString");
        System.out.println(orderNumber);
        resp.getWriter().print(orderNumber);
    }
}

And a picture with responses and how I am doing it:

enter image description here

EDIT

As was commented by Mukesh Verma.

All I had to do was add @MultipartConfig Annotation and I got the data.

Richard
  • 1,087
  • 18
  • 52

3 Answers3

1

This is not how method getParameter works. As stated in this question, you should call the servlet with the following URL:

http://localhost:8080/api/form?testString=test
Lorelorelore
  • 3,335
  • 8
  • 29
  • 40
1

Try using @MultipartConfig Annotation. It handles form-data mime type.

Mukesh Verma
  • 524
  • 5
  • 9
0

Changing Postman's radiobutton from form-data to x-www-form-urlencoded also does the trick and I am able to get the data.

Siva
  • 170
  • 1
  • 3
  • 11