Restful DELETE method

Asked

Viewed 1,525 times

3

Within the Rest architecture we have the verbs POST, GET PUT and DELETE. Among the various examples and tutorials I studied. The use of the verbDELETE is always exemplified with DELETE /addresses/1, where I basically pass the address and the ID than I want to delete. However, in cases where I need to pass more parameters in the URL like DELETE /addresses/id/codUsuario. This is also correct within the structure REST?

2 answers

3

Short answer: yes.


The RFC specifying the method DELETE describes it as follows:

"In fact, this method is similar to the rm command on UNIX: it expresses an exclusion operation in the URI mapping of the source server, rather than an expectation that previously associated information will be deleted." (translated via Google Translate)

As such it is perfectly feasible to delete resources that are associated with other resources in the same way that it is possible to do rm of files/directories that are inside other directories.

It must always be taken into account that if the appeal is deleted father it may not make sense for the remaining resources to exist. Taking the example of the question, it makes sense to delete an address without deleting a user but it makes no sense to delete a user without deleting their addresses.

In the Mozilla Developer Network it appears that an application DELETE can contain a body but this practice goes against what was originally described in RFC and it is not guaranteed to be supported by all implementations of the method DELETE.

2


The idea itself is valid.

I haven’t seen any problems since id and codUsuario form some kind of composite key to access this resource in which you want to remove OR both form a URL in which the set of these two information can point to a single resource, in a parent-child relationship.

I will give an example of each case. First, with composite key.

First example

If you want to remove a document in which the composite key is document type and its number:

/documentos/{tipo-documento}/{numero}

A request would be:

DELETE /documentos/CPF/12312312300/

Now let’s go to the second example.

Second example

You may have orders and products, and wish to access the product within an order (or remove it in your case).

/pedidos/{id-pedido}/produtos/{id-produto}

And the request:

DELETE /pedidos/123/produtos/3333

However, looking at the example he gave, his endpoint is very strange, it does not seem to be either of the two cases because it is not possible to see a natural relationship between id and codUsuario. We would need to understand the context behind this endpoint for a better solution. Perhaps, you are looking for something like this:

DELETE /users/{codUsuario}/addresses/{idAddress}
  • Yes, for me it also makes a lot of sense to use this way. I would just like to be sure to be following the REST standard. In my case I have a composite key N for N. Where it would make no sense to use an id for this table. At least, by default it is not used. An example of one of these tables I have is post_like, where the identifiers are usuario_id and post_id. Therefore, to excuse a post you would need to use a DELETE command /url+route/usuario_id/post_id

  • @Alan, if your intention is to like/neglect a post, I think you’ll be interested in seeing how Github controls star/unstar in their API.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.