Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring REST controller post request

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?

like image 437
Steve Avatar asked Apr 02 '15 22:04

Steve


People also ask

How to test Spring Boot resttemplate POST request?

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

How do I send a POST request to the rest controller?

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 ...

What is @restcontroller annotation in Salesforce?

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.

How do I send a POST request in Spring Boot?

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.


1 Answers

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:

  1. The request is an HTTP or HTTPS request.
  2. The HTTP method is POST.
  3. The content type is application/x-www-form-urlencoded.
  4. 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
like image 166
Sotirios Delimanolis Avatar answered Oct 27 '22 11:10

Sotirios Delimanolis