web-services - 具有只读元素的资源的 RESTful 部分更新?

标签 web-services rest

我正在设计一个 RESTful API,该 API 目前拥有的资源包括一些可以由客户端更新的元素和其他不能由客户端更新的元素。举个例子:

{
    id : "1234",
    firstName  : "George",
    lastName   : "Burdell",

    blogPosts : { href : "http://server.com/user/1234/blogposts"}
}

API 的客户端可以 PUT 新资源或 PATCH 现有资源,但在示例中,他们只能写入 firstNamelastNameidblogPosts 由服务器生成,客户端不可修改。

处理尝试写入不可写字段的推荐方法是什么?返回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.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

关于web-services - 具有只读元素的资源的 RESTful 部分更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9470429/

相关文章:

java - 如何在 Rest 请求体内使用带有单字符序列构造函数/工厂方法的对象而不是单字符串构造函数?

ios - REST API iOS 开发

java - GrizzlyHttpServerFactory.createHttpServer @GET 带参数

java - 通过 JAX-RS 实现 REST 服务的可重用性

vb.net - 将数据集从 asmx 文件返回到 Windows 窗体

rest - 如何使用 REST API 通过指针查询对象?

c# - 共享 Windows 主机上的 Windows 服务

java - 我如何使用带有 json 参数的curl 调用Web 服务?

rest - REST API 应该区分大小写还是不区分大小写?

javascript - 使用 caml 查询获取列表项剩余调用