4

I never really understood why do I need the PUT and Delete request methods.

In my code I'm using all the time post and just do the backend code to add/delete something.

Am I doing this wrong or is it ok to use POST all the time?

My example:

  @RequestMapping(value = "/delete-images", method = RequestMethod.POST)
public @ResponseBody void deleteImages(@RequestParam("imageIDs") String[] imageIDs) {
    Docker.deleteImages(imageIDs);
}
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 2
    It conveys meaning. Post means to add, Put means to update. Delete is self explanatory – mbx-mbx Feb 21 '18 at 12:19
  • You can do the same work with POST also but the rest specifications tells you to use PUT or DELETE for cases – Arpit Solanki Feb 21 '18 at 12:19
  • 2
    they invented it for some reason, otherwise people would just use post all the time. Is there any advantages or is it just the usual way of doing things? –  Feb 21 '18 at 12:22
  • 1
    Its about semantics and conveying meaning with your requests. It's also about following set standards (i.e. a common set of rules we adhere to) so that when another dev comes in they can understand what is going on. When everyone is 'on the same page' things are easier as there is common ground to work with. – mbx-mbx Feb 21 '18 at 12:30
  • Why even distinguish between GET and POST? Why not do everything with just one/no verb? … Well, why not also add a few other verbs? – deceze Feb 21 '18 at 12:35
  • Personally I think that PUT and DELETE (not to mention PATCH) are outdated relics. What if my API endpoint needs to do multiple of those functions at once? Am I then 'forced' to split my endpoint into two or three smaller ones, creating a transaction problem that did not exist before? No way, we use POST which (to all the many teams that I worked with) signals in a broad sense that we are changing data on the server. Also the URL will be named so clearly that confusion will not happen. – Peter B Feb 21 '18 at 12:42
  • @user9385638 Actually, until REST came along, people **did** use just post all the time. – biziclop Feb 21 '18 at 12:59
  • 1
    Read [ask] and share your research. https://stackoverflow.com/questions/4573305/rest-api-why-use-put-delete-post-get, https://stackoverflow.com/questions/23777714/actual-use-of-get-put-delete-post-methods-in-http, https://stackoverflow.com/questions/12142652/what-is-the-usefulness-of-put-and-delete-http-request-methods, https://stackoverflow.com/questions/630453/put-vs-post-in-rest – CodeCaster Feb 21 '18 at 13:07

2 Answers2

11

The idea of REST is that you design your endpoints in a certain way, to represent the logical structure of the things you manipulate, then you use HTTP verbs to express what type of manipulation you're performing.

So for example rather than having /get-image?imageId=X and /delete-image?imageId=X endpoints, you should have just an /image/X endpoint (notice the absence of query parameters), and then the verbs GET/PUT/DELETE encode what you actually want to do with the image.

There are plenty of advantages of this approach (caching, standard tools, etc.), but there are two caveats:

  1. Not every data/operation fits this hierarchical resource model required by REST. Trying to squeeze it in regardless may result in very awkward APIs. (One example of something that doesn't quite fit is bulk operations.)
  2. The two things (URLs identify actual resources and verbs are used to identify operations) work in tandem, one without the other makes little sense.
biziclop
  • 48,926
  • 12
  • 77
  • 104
1

It's all about the semantics of the request. From the RFC 7231:

The request method token is the primary source of request semantics; it indicates the purpose for which the client has made this request and what is expected by the client as a successful result.

Here's a brief description of some HTTP methods defined in the RFC 7231 (click the links to check the full method definition):

  • GET: Transfer a current representation of the target resource.
  • HEAD: Same as GET, but only transfer the status line and header section.
  • POST: Perform resource-specific processing on the request payload.
  • PUT: Replace all current representations of the target resource with the request payload.
  • DELETE: Remove all current representations of the target resource
Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359