如何区分服务不存在或记录不足? [英] How to tell between a nonexistent service or a lack of a record?

查看:55
本文介绍了如何区分服务不存在或记录不足?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在提供一些REST服务,并且在实现GET请求时,我正在辩论如果那里没有记录(例如/profile/1234)该怎么办.现在,如果您尝试使用/profiles/1234(请注意配置文件中的),则肯定是404代码,因为绝对没有找到该URL.但是当涉及到没有1234配置文件时,我不确定应该返回什么.我不想返回{},因为这可能会给人留下没有数据记录的印象.但是,如果我返回404错误代码,API的使用者应该如何分辨这两个404之间的区别?

I'm making some REST services, and as I'm implementing a GET request, I was debating about what I should do if there is no record there, for example /profile/1234. Now, if you tried to use /profiles/1234 (note the s on profiles) it would definitely be a 404 code because that URL definitely isn't found. But when it comes to there just being no 1234 profile, I'm not sure what I should return. I don't want to return {} because that might give the impression there is a record with no data. But if I return a 404 error code, how is the consumer of the API supposed to tell the difference between the two 404s?

如何通过编程方式沟通不存在的服务和不存在的记录之间的区别,如何成为负责任的API设计人员?

How can I be a responsible API designer by communicating programmatically what the difference between a service that doesn't exist and a record that doesn't exist?

推荐答案

您可以发送自定义状态消息,以帮助您区分不同"的404 s.

You can send a custom status message which will help you differentiate between the "different" 404s.

另一方面,我不会担心这种区别.人们习惯于必须仔细输入api端点.只要他们手动对应用程序进行一点测试,问题就不会出现.

On the other hand, I wouldn't worry about that distinction. People are used to having to type api endpoints carefully. As long as they manually test their app a tiny bit the issue becomes a non-issue.

这篇关于如何区分服务不存在或记录不足?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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