本文介绍了404是否是当前不存在的资源的正确返回码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计用于调查交付的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 OK).

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的行为必须不一致.或是否有多个可用调查—最好创建一个获取调查列表" 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是否是当前不存在的资源的正确返回码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 02:03