Reputation: 513
I have this controller in spring
@RestController
public class GreetingController {
@RequestMapping(value = "/greeting", method = RequestMethod.POST)
public String greeting(@RequestParam("uouo") String uouo) {
return uouo;
}
}
and when I testing it
curl -k -i -X POST -H "Content-Type:application/json" -d uouo=test http://192.168.1.104:8080/api/greeting
the result of the testing
HTTP Status 400 - Required String parameter 'uouo' is not present
I tried may thing, but I think @RequestParam
can't use for POST it always passed the parameter in URL using GET, I use post only if I had object JSON as parameter using @RequestBody
, is there any way to make string parameter send using POST?
Upvotes: 5
Views: 16296
Reputation: 279990
The Servlet container will only provide parameters from the body for POST requests if the content type is application/x-www-form-urlencoded
. It will ignore the body if the content type is anything else. This is specified in the Servlet Specification Chapter 3.1.1 When Parameters Are Available
The following are the conditions that must be met before post form data will be populated to the parameter set:
- The request is an HTTP or HTTPS request.
- The HTTP method is POST.
- The content type is
application/x-www-form-urlencoded
.- The servlet has made an initial call of any of the
getParameter
family of methods on the request object.If the conditions are not met and the post form data is not included in the parameter set, the post data must still be available to the servlet via the request object’s input stream. If the conditions are met, post form data will no longer be available for reading directly from the request object’s input stream.
Since you aren't sending any JSON, just set the appropriate content type
curl -k -i -X POST -H "Content-Type:application/x-www-form-urlencoded" -d uouo=test http://192.168.1.104:8080/api/greeting
or let curl
infer it
curl -k -i -X POST -d uouo=test http://192.168.1.104:8080/api/greeting?uouo=test
Note that you can still pass query parameters in the URL
curl -k -i -X POST -H "Content-Type:application/json" http://192.168.1.104:8080/api/greeting?uouo=test
Upvotes: 8