Reputation: 2025
What is the best practise for api return values for basic db operations while we are trying to add,delete,create,update etc..?
1-(If exists what is most suitable status codes for these operations?)Just returning corresponding http status code?(e.g. return 204 for delete operation,201 create operation,500 for "data does not exist" etc.)
2-Return boolean
3-Return custom exception
Shortly,Assume we have project A and it is using api of project B. And B is trying to delete a record from db with a spesific id, but this id does not exist in db.For such scenario, what should we do ?
Upvotes: 0
Views: 39
Reputation: 827
1-(If exists what is most suitable status codes for these operations?)Just returning corresponding http status code?(e.g. return 204 for delete operation,201 create operation,500 for "data does not exist" etc.)
Returning 204 for deleting and 201 for creation is absolutely fine, but just try to think about what is the most descriptive response code from the perspective of the requester. Also, remember that 500 response codes are used for when something is wrong on the server side (e.g. an unexpected exception).
Shortly,Assume we have project A and it is using api of project B. And B is trying to delete a record from db with a spesific id, but this id does not exist in db.For such scenario, what should we do ?
Again, think about what would be the most helpful error to the requester. 404 Not Found would accurately describe why the operation failed. You can place an error message in the response body if you want to make it more descriptive - but you don't have to.
Upvotes: 1