Starting Engineering Quiz Section. I bring you a question related to Architecture, REST APIs, SOLID, anything that helps us reflect and debate!
The first one is about RESTful APIs 😄
Given a RESTful API and the sequence of Requests, which is the Status Code from the last call?
Some possible answers:
Bad Request. 400
Not Found. 404
I will post the answer by Friday in the same Thread :D
For this scenario, the result can be either 200, as @Volodymyr shared, and 404, as Adrián Lizaga shared, because DELETE is an idempotent operation but the response can be different.
Check out the Wikipedia's article here https://en.wikipedia.org/wiki/Representational_state_transfer#Semantics_of_HTTP_methods
The GET, PUT, and DELETE methods are idempotent, meaning that applying them multiple times to resource results in the same state change of the resource as applying them once, though the response might differ
In case we are in a distributed system scenario, I prefer the 200 as a response code. Why? Because from the user's perspective, they want to delete an item. If for any reason the item is already deleted, it has the right response the system no longer contains that item.
the system no longer contains that item
Yet, if you compare the RESTful implementation to other Resource Containers such as Java Lists or SQL, you will find on them that, when you delete an item that doesn’t exist, the operation success and they don’t throw any exception.
So, why add the logic of failing to an operation when the other implementation doesn’t do and yet the final state is the desired state?
200 for idempotent deletion
404 because DELETE is idempotent and the resource doesnt exists (the response is different but there isnt any change of state)
404? The resource does not exist anymore :D