Is 404 the right return code for a resource that c

2019-07-11 14:26发布

问题:

I am designing an API for survey delivery. Every once in a while, a client will poll the survey and check if there is a survey available for that user.

If the survey exists, I will return it (200 OK).

But if there is no survey scheduled at the moment, is returning a 404 appropriate?

It is true that the resource doesn't exist, but at the same time, 404 is "client error", and it feels wrong to return it, since no error has occurred. The response is meant to say "No survey at the moment. Check again later". What would be an appropriate response code?

回答1:

Instead of a "get survey" API, which then has to behave inconsistently if there's no survey available yet — or if there are multiple surveys available — it might be better to create a "get list of surveys" API, that is always successful regardless of whether this list is empty, contains exactly one element, or contains multiple elements.



标签: api rest http