使 PUT 在 REST 中创建请求幂等 API
Making PUT create request idempotent in REST API
我的目标是使幂等/创建 REST API 实现为 PUT 动词。
Idempotent RFC 状态:
Idempotent methods are distinguished because the request can be
repeated automatically if a communication failure occurs before the
client is able to read the server's response. For example, if a
client sends a PUT request and the underlying connection is closed
before any response is received, then the client can establish a new
connection and retry the idempotent request. It knows that repeating
the request will have the same intended effect, even if the original
request succeeded, though the response might differ.
PUT RFC 状态:
If the target resource does not have a current representation and the
PUT successfully creates one, then the origin server MUST inform the
user agent by sending a 201 (Created) response. If the target
resource does have a current representation and that representation
is successfully modified in accordance with the state of the enclosed
representation, then the origin server MUST send either a 200 (OK) or
a 204 (No Content) response to indicate successful completion of the
request.
假设 /create 将创建的资源存储在数据库中,是否应该 return 第一次创建时为 201,重试 /create 时为 200?
是否应该重试/创建再次在数据库中存储相同的资源以符合 PUT RFC?
所以这个问题有点迷糊。让我们看看能不能解开它。
PUT /create
abcde
大致说:用表示abcde
替换/create
的状态。换句话说,消息的语义类似于
store(key => "/create", value => "abcde")
请注意,处理此消息两次与处理一次消息产生的效果相同。
store(key => "/create", value => "abcde")
store(key => "/create", value => "abcde")
请注意,我们对此处使用的密钥非常具体; PUT 与 目标资源 的状态有关; PUT /create
是请求我们修改 /create
的消息,而不是请求我们创建一些 other 资源。
Assuming that /create stores the created resource in DB, should it return 201 on first creation and 200 on retried /create?
是的。
Should retried /create store the same resource in DB all over again to conform with PUT RFC?
如果资源已经有请求的表示,则不需要再次存储它。
我的目标是使幂等/创建 REST API 实现为 PUT 动词。
Idempotent RFC 状态:
Idempotent methods are distinguished because the request can be
repeated automatically if a communication failure occurs before the
client is able to read the server's response. For example, if a
client sends a PUT request and the underlying connection is closed
before any response is received, then the client can establish a new
connection and retry the idempotent request. It knows that repeating the request will have the same intended effect, even if the original
request succeeded, though the response might differ.
PUT RFC 状态:
If the target resource does not have a current representation and the PUT successfully creates one, then the origin server MUST inform the
user agent by sending a 201 (Created) response. If the target
resource does have a current representation and that representation
is successfully modified in accordance with the state of the enclosed representation, then the origin server MUST send either a 200 (OK) or a 204 (No Content) response to indicate successful completion of the
request.
假设 /create 将创建的资源存储在数据库中,是否应该 return 第一次创建时为 201,重试 /create 时为 200? 是否应该重试/创建再次在数据库中存储相同的资源以符合 PUT RFC?
所以这个问题有点迷糊。让我们看看能不能解开它。
PUT /create
abcde
大致说:用表示abcde
替换/create
的状态。换句话说,消息的语义类似于
store(key => "/create", value => "abcde")
请注意,处理此消息两次与处理一次消息产生的效果相同。
store(key => "/create", value => "abcde")
store(key => "/create", value => "abcde")
请注意,我们对此处使用的密钥非常具体; PUT 与 目标资源 的状态有关; PUT /create
是请求我们修改 /create
的消息,而不是请求我们创建一些 other 资源。
Assuming that /create stores the created resource in DB, should it return 201 on first creation and 200 on retried /create?
是的。
Should retried /create store the same resource in DB all over again to conform with PUT RFC?
如果资源已经有请求的表示,则不需要再次存储它。