问题描述
我正在设计一个用于调查交付的API。每隔一段时间,客户就会对调查进行一次调查,并检查是否有针对该用户的调查。
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.
如果调查存在,我会将其退回(200好的)。
If the survey exists, I will return it (200 OK).
但是如果目前没有安排调查,那么返回适当的404吗?
But if there is no survey scheduled at the moment, is returning a 404 appropriate?
确实资源不存在,但同时404是客户端错误,返回它感觉不对,因为没有发生错误。回应是为了说目前没有调查。稍后再检查。什么是适当的响应代码?
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?
推荐答案
而不是获取调查API,如果有的话,它必须表现不一致暂无调查—或者如果有多个调查可用—创建一个获取调查列表API可能会更好,无论此列表是空的,只包含一个元素,还是包含多个元素,它总是成功的。
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.
这篇关于404是否是当前不存在的资源的正确返回码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!