REST API PATCH 请求预期行为

标签 rest api http-patch

我不确定以下情况的预期行为是什么:

补丁请求 1:

"body": {
    "un_updatable_field" : "data" 
}

所以在这里我只是抛出一个异常:Field cannot be updated , 任何。

补丁请求 2:
"body": {

    "all_required_fields" : "all",
    "un_updatable_field" : "data" 
}

我应该在这里做什么?抛出异常并且不更新模型?

最佳答案

Patch操作应该是原子的,每 the spec :

The server MUST apply the entire set of changes atomically and never provide (e.g., in response to a GET during this operation) a partially modified representation. If the entire patch document cannot be successfully applied, then the server MUST NOT apply any of the changes. The determination of what constitutes a successful PATCH can vary depending on the patch document and the type of resource(s) being modified. For example, the common 'diff' utility can generate a patch document that applies to multiple files in a directory hierarchy. The atomicity requirement holds for all directly affected files. See "Error Handling", Section 2.2, for details on status codes and possible error conditions.


看起来你的具体情况是

Unprocessable request: Can be specified with a 422 (Unprocessable Entity) response ([RFC4918], Section 11.2) when the server understands the patch document and the syntax of the patch document appears to be valid, but the server is incapable of processing the request. This might include attempts to modify a resource in a way that would cause the resource to become invalid; for instance, a modification to a well-formed XML document that would cause it to no longer be well-formed. There may also be more specific errors like "Conflicting State" that could be signaled with this status code, but the more specific error would generally be more helpful.

409 Conflict也可能是合适的,具体取决于无法修改资源的原因。

关于REST API PATCH 请求预期行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52520703/

相关文章:

ruby-on-rails - 如何在 Rails 4 中将 PDF 转换为 Excel 或 CSV

java - 支持 "PATCH"动词 - WSHttpHandler handleExchange 给出警告 : Cannot handle HTTP method: PATCH

azure - 使用 API 管理服务及其 API 的 ARM 模板进行部署时出现问题

javascript - 按点时提示方法/字段

odata - 在 CRUD 操作期间,Odata 适配器是否支持 "Patch"请求和 "$expand"查询?

rest - 击中除索引以外的任何内容时,将使REST 404失效

java - Spring 从 Http 重定向到 Https 中断 Rest Controller 测试

java - Spring Rest 模板 - 将请求字符串主体发布到 azure 给出 500 错误

javascript - 无法在 Sails JS 中创建版本化 API