0

From my understanding when Request Method is Get we should not have Request body, we use @RequestParam to read the url parameters. But spring test seems to be allowing it. Below is a sample code.

@Controller
public class SomeController {

    @RequestMapping(value = "/abc", method = RequestMethod.GET)
    public String greeting(@RequestBody Student student) {
        return "Hello "+ student.name;
    }

}

This is the test code and jsonString is the string representation of a Student object.

@Test
    public void shouldReturnMessage() throws Exception {
        this.mockMvc.perform(get("/abc").content(jsonString)).andDo(print()).andExpect(status().isOk());
    }

My question here is, should Spring stop allowing @RequestBody with Get request? The above test runs fine and it's able to return me the student name. But when I try to call the same endpoint using curl it throws an error.

Reference : HTTP GET with request body

  • 1
    Any HTTP request can have a body, so no. https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages – The Head Rush Jan 13 '21 at 15:19
  • Actually this doubt came into my mind when I was trying to access the same endpoint with my ReactJs application and axios. I tried to pass the body and it's was not getting passed from the browser. So, my immediate concern was how the spring -test passed. From what I understand going through this [Link](https://github.com/axios/axios/issues/787) is that `XHR` does not allow. But, seems like Spring allows which seems odd. You can take a look at the same link you shared [Here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET) – ArkoSarkar Jan 15 '21 at 16:50

0 Answers0