如何区分服务不存在或记录不足?

编程入门 行业动态 更新时间:2024-10-22 02:45:26
本文介绍了如何区分服务不存在或记录不足?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在提供一些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.

更多推荐

如何区分服务不存在或记录不足?

本文发布于:2023-10-25 08:47:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!