0
@ResponseBody

@RequestMapping(value = {"apiRequest"}, method = {RequestMethod.POST})

public String contestApiSignUp(HttpServletRequest req) throws JSONException {
try {
    String username = req.getParameter("username");
    String firstname = req.getParameter("firstname");
    String lastname = req.getParameter("lastname");
    String password = req.getParameter("password");
    String phone = req.getParameter("phone");

Here the values I am getting all are null. That is username =null, firstname =null...

I am hitting post request with the values http://localhost:8080/apiRequest.htm

username = Subhajit
firstname = Subha
...

like this.

But while I am using same code but,

@RequestMapping(value = {"apiRequest"}, method = {RequestMethod.GET})

using GET instead of POST then I am getting the proper values.

shilovk
  • 11,718
  • 17
  • 75
  • 74
smondal345
  • 65
  • 1
  • 10

2 Answers2

0

When you sent your HTML Form by POST to your URL /apiRequest, did you fill your data (username, firstname etc.) in fields in your form, or did you attached them to the sent URL (eg. apiRequest.htm?username=test&...) ?

Because with the second approach, you data will be accessible only through a GET request, not a POST. The first approach will work with a POST request.

Also, you do not need to call explicitly getParameter on the HttpServletRequest in a Spring Controller.

You can do this :

@ResponseBody
@RequestMapping (value = "apiRequest", method = RequestMethod.POST)
public String contestApiSignUp(
        @RequestParam ("username") String username,
        @RequestParam ("firstname") String firstname,
        @RequestParam ("lastname") String lastname,
        @RequestParam ("password") String password,
        @RequestParam ("phone") String phone) {
    return "Hello World";
}
Adrien Ferrand
  • 726
  • 5
  • 13
  • Thank u so much for your reply. " apiRequest.htm?username=test& " I am not posting the data like this. I am using postman setting the params as headers and its values. – smondal345 Oct 28 '16 at 13:45
  • 1
    Ok, I never used postman, but it should work like other API request tools. You should not try to pass arbitrary key/value pairs in the headers of your request. These headers are for the metadata of the request, not the data. To pass data in a POST request, you must build a request body containing theses key/value pairs. Check the doc of postman, you can do so with a form-like interface. See https://www.getpostman.com/docs/requests, section Request Body > form-data. – Adrien Ferrand Oct 28 '16 at 14:37
0

From the comments, you said you are trying with postman to post the request, So are you building website or webservices? either way you should not be sending your data in the headers.

  • If you are building a website put together an HTML file from which you can submit a form to this controller
  • If you are building webservice, change the controller and let it accept JSON (or XML -I would discourage this though) body. Follow this https://stackoverflow.com/a/40226580/6785908. And then from postman (or curl), post a json instead.
Community
  • 1
  • 1
so-random-dude
  • 15,277
  • 10
  • 68
  • 113