我正在设计一个 RESTful API,该 API 目前拥有的资源包括一些可以由客户端更新的元素和其他不能由客户端更新的元素。举个例子:
{
id : "1234",
firstName : "George",
lastName : "Burdell",
blogPosts : { href : "http://server.com/user/1234/blogposts"}
}
API 的客户端可以 PUT 新资源或 PATCH 现有资源,但在示例中,他们只能写入 firstName
和 lastName
。 id
和 blogPosts
由服务器生成,客户端不可修改。
处理尝试写入不可写字段的推荐方法是什么?返回401并忽略整个更新文档? 401 是合适的响应吗?
拥有同时包含可写和不可写元素的资源是一个坏主意吗? (我对此很陌生,但似乎这通常是不可避免的,特别是在链接到相关资源的示例中)。
最佳答案
返回 403 并且不更新资源。
403 Forbidden
The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. If the server does not wish to make this information available to the client, the status code 404 (Not Found) can be used instead.
关于web-services - 具有只读元素的资源的 RESTful 部分更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9470429/