我正在提供一些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.
更多推荐
如何区分服务不存在或记录不足?
发布评论