0

While reading difference between PUT and POST, I came across this answer which says:

An HTTP PUT is supposed to accept the body of the request, and then store that at the resource identified by the URI.

An HTTP POST is more general. It is supposed to initiate an action on the server. That action could be to store the request body at the resource identified by the URI, or it could be a different URI, or it could be a different action.

And now while going through this tutorial, I found this:

@RequestMapping(value = EmpRestURIConstants.DELETE_EMP, method = RequestMethod.PUT)
public @ResponseBody Employee deleteEmployee(@PathVariable("id") int empId) {
    logger.info("Start deleteEmployee.");
    Employee emp = empData.get(empId);
    empData.remove(empId);
    return emp;
}

I was thinking if I am using PUT I should be using @RequestBody. But this doesnt seem to be the case. And above method seem to use PUT very much like GET, right? Or I am missing anything here?

Q1. What are clear purpose of these methods, or at least when certain methods "cannot" be used?

Q2. Is it like, if I am using GET, then I cannot use @RequestBody, as GET request doesnt contain any and I am left with using only @RequestParam and @PathVariable and PUT and POST can use all. And irrespective of methods all can return @ResponseBody. If all these guessings are correct, is their any official documentation explaining all these restrictions, including for other methods too?

Community
  • 1
  • 1
MsA
  • 2,599
  • 3
  • 22
  • 47
  • So you think deleting an employee is a good candidate for a GET request... – M. Deinum Oct 31 '18 at 14:27
  • 2
    its not spring. You should first understand usage of HTTP Methods. – want2learn Oct 31 '18 at 14:29
  • ohh so deleting here is actually interpreted as rewriting the target object with empty one making it suitable for PUT? But then why not use `DELETE` method? – MsA Oct 31 '18 at 14:30
  • Could have been a DELETE as well I guess, but DELETE has the same "issue" as GET as it has no body. With PUT (and POST) you can have a body. – M. Deinum Oct 31 '18 at 14:38
  • so basically here there is no hard fast rule. The only thing (apart from conventions and method's inherent meaning) that determines which method to use is whether there is request body, in which case cannot at all use GET, right? Are there any other restrictions (above Q2)? – MsA Oct 31 '18 at 14:42

2 Answers2

1

So I have never given more thought about this, and pretty much always used either GET or POST, but after reading this blog, it seems much clear when to use PUT over POST. Here I will be quoting the blog.

What Does the PUT Method Do?

The PUT method completely replaces whatever currently exists at the target URL with something else. With this method, you can create a new resource or overwrite an existing one given you know the exact Request-URI. An example of a PUT method being used to create a new resource would resemble the following:

PUT /forums/<new_thread> HTTP/2.0
Host: https://yourwebsite.com/

Where would be the actual name or ID number of the thread. Alternatively, a PUT method used to overwrite an existing resource could look like this:

PUT /forums/<existing_thread> HTTP/2.0
Host: https://yourwebsite.com/

In short, the PUT method is used to create or overwrite a resource at a particular URL that is known by the client.

What Does the POST Method Do?

The HTTP POST method is used to send user-generated data to the web server. For example, a POST method is used when a user comments on a forum or if they upload a profile picture. A POST method should also be used if you do not know the specific URL of where your newly created resource should reside. In other words, if a new forum thread is created and the thread path is not specified then you could use some like:

POST /forums HTTP/2.0
Host: https://yourwebsite.com/

Conclusion

So after reading this, and seeing your example, it makes a lot of sense why you are using PUT and not POST, and it has to do with your URL. Since you are using a @PathVariable, it means you have complete knowledge of of the exact Request-URI. Otherwise, using POST would of made more sense.

Alain Cruz
  • 4,757
  • 3
  • 25
  • 43
0

Below is a table summarizing recommended return values of the primary HTTP methods in combination with the resource URIs

enter image description here

gauravoli
  • 74
  • 1
  • 3
  • 9