REST API PATCH 请求预期行为
REST API PATCH Request expected behavior
我不确定以下情况下的预期行为是什么:
补丁请求 1:
"body": {
"un_updatable_field" : "data"
}
所以在这里我只是抛出一个异常:Field cannot be updated
,不管怎样。
补丁请求 2:
"body": {
"all_required_fields" : "all",
"un_updatable_field" : "data"
}
我应该在这里做什么?抛出异常而不更新模型?
我假设 un_updateable_field
是您系统中存在的字段,但您不想让人们更新它。
您可以选择忽略,也可以选择抛出错误。你应该做什么取决于你。我更喜欢我的系统是严格的,而不是忽略无效值,因为如果出现无效值,它可能表明你在某个地方有错误,最好得到一个硬错误,这样你就可以修复这个错误。
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.
a 409 Conflict
也可能是合适的,具体取决于无法修改资源的原因。
我不确定以下情况下的预期行为是什么:
补丁请求 1:
"body": {
"un_updatable_field" : "data"
}
所以在这里我只是抛出一个异常:Field cannot be updated
,不管怎样。
补丁请求 2:
"body": {
"all_required_fields" : "all",
"un_updatable_field" : "data"
}
我应该在这里做什么?抛出异常而不更新模型?
我假设 un_updateable_field
是您系统中存在的字段,但您不想让人们更新它。
您可以选择忽略,也可以选择抛出错误。你应该做什么取决于你。我更喜欢我的系统是严格的,而不是忽略无效值,因为如果出现无效值,它可能表明你在某个地方有错误,最好得到一个硬错误,这样你就可以修复这个错误。
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.
a 409 Conflict
也可能是合适的,具体取决于无法修改资源的原因。