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?
In this Spring Boot RestTemplate POST request test example, we will create a POST API and then test it by sending request body along with request headers using postForEntity () method. 1. Maven dependencies
To manually test this, I used Postman to send a POST request to the Rest Controller: 1 Set the request type to POST 2 Set the content type in the header to application/json; charset=UTF-8 3 Add the JSON for the PersonDTO to the body of the request (in the raw option) 4 Add the request path 5 Press send More ...
This annotation is used at the class level and allows the class to handle the requests made by the client. Let’s understand @RestController annotation using an example. The RestController allows to handle all REST APIs such as GET, POST, Delete, PUT requests.
Simple Spring Boot: Post. 1 Set the request type to POST. 2 Set the content type in the header to application/json; charset=UTF-8. 3 Add the JSON for the PersonDTO to the body of the request (in the raw option) 4 Add the request path. 5 Press send.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With