Is it advisable to return 404 in an API call in th

2019-08-03 18:49发布

问题:

Say there is an api call /super-api/calendar/date/events. The resource "events" is created only when you add one or more events to a particular date in the calendar. Currently, the api returns 404 when there are no events in the calendar. My thought it that the api should return zero events or something like that instead of 404. But, I am not 100% sure. The reason - 404 could be generated because the url is wrong or the resource name changed. Please advise.

Edit - I believe that the underlying code should be changed to generate "events" resource for every date in the calendar instead of waiting to do that when a user adds an event to a date.

Related question - REST API 404: Bad URI, or Missing Resource?

Unfortunately, the related question does not answer my specific scenario. Hence, I posted this as a new question. I would actually like to post this as a comment instead, but I cant because I am new here.

回答1:

You're right, you should return an empty list instead of 404 error (you could return 204 though). You could return 404 in queries like this one in case there is no event with id 2.

/super-api/calendar/date/events/2