在这种情况下,是否建议在 API 调用中返回 404? [英] Is it advisable to return 404 in an API call in this case?

查看:30
本文介绍了在这种情况下,是否建议在 API 调用中返回 404?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说有一个api调用/super-api/calendar/date/events.仅当您将一个或多个事件添加到日历中的特定日期时,才会创建资源事件".当前,当日历中没有事件时,api 返回 404.我认为 api 应该返回零事件或类似的东西而不是 404.但是,我不是 100% 确定.原因 - 404 可能是因为 url 错误或资源名称更改而生成的.请指教.

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.

相关问题 - 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.

推荐答案

你说得对,你应该返回一个空列表而不是 404 错误(你可以返回 204 虽然).如果没有 id 为 2 的事件,您可以在这样的查询中返回 404.

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

这篇关于在这种情况下,是否建议在 API 调用中返回 404?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆