Restful method DELETE

2

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

    
asked by anonymous 19.04.2018 / 15:58

2 answers

2

Short answer: yes.

The RFC that specifies the DELETE method describes it as follows: / p>

  

"In fact, this method is similar to the rm command on UNIX: it expresses a delete operation on the source server's URI mapping, rather than an expectation that previously associated information is 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 should always be taken into account that if parent is deleted, 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 does not make sense to delete a user without deleting their addresses.

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

    
19.04.2018 / 16:34
1

The idea itself is valid.

I do not see problems since id and codUsuario are 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 compound key.

First example

If you want to remove a document in which the compound key is the 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 can have the orders and products, and you want to access the product within an order (or remove it in your case).

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

And a request:

DELETE /pedidos/123/produtos/3333

However , looking at the example you gave, your endpoint is very strange, it does not seem to be either case since you can not see a natural relationship between id and codUsuario . We would need to understand the context behind this endpoint for a better solution. Maybe you're looking for something like this:

DELETE /users/{codUsuario}/addresses/{idAddress}
    
19.04.2018 / 17:20