This is basically best practice question. In my Rest API (that I am currently working on) I have routes matching PUT
and DELETE
requests on URLs like /resources/{resourceID}
. Behind the API lies SQL DB.
In general, executing a DELETE
or UPDATE
query on non-existing resource ID in SQL databases gives no error, only the affectedRows
returns 0.
Now I have a question on how to perform such request properly:
UPDATE
or DELETE
(which involves additional SQL query)UPDATE
/ DELETE
query without checking for existence and inspect the result of SQL query and return HTTP 404
in case when affectedRows == 0
?I personally prefer the later one. But I can think of other DBMS (not SQL or not relational) where performing INSERT
, UPDATE
or DELETE
queries may not return any information about affected rows. How to proceed in such cases?
Once you are performing DELETE
and UPDATE
operations using the resource ID, I cannot see any problems on checking the number of affected rows. This should be enough to check whether your resource exists or not.
Basically:
DELETE
or an UPDATE
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With