如果POST成功但未创建任何新内容,我们应该返回什么状态代码? [英] What status code should we return if the POST succeeds but does not result in creating anything new?

查看:47
本文介绍了如果POST成功但未创建任何新内容,我们应该返回什么状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个端点,当您发布时会创建一个新版本的资源,并返回201和新创建的资源的位置.它根据当前版本与发布版本的比较来确定新版本号(使用类似规则集之类的符号).

We have an endpoint which when you post create a new version of resource and returns a 201 and the location of the newly created resource. It determines the new version number based on a comparison of the current version and the version being posted (using a semver like ruleset).

如果您发布的版本与现有版本相同,则不会更新任何版本号.在这种情况下,我们应该返回什么?

If the version you post is identical to the existing version then no version number is updated. What should we return in this case?

  • 即使我们在技术上还没有创建任何东西,我们也可以返回201.
  • 我不想返回409,因为它并不是真正的冲突,例如当您发布具有相同ID的东西时.如果您在现有版本稍有不同的情况下发布相同的内容,那么您将很高兴获得201.
  • 我们可以返回200,但这看起来很奇怪,并增加了用户必须处理的响应代码

201响应的幂等性重要吗?

Does the idempotency of the 201 response matter?

还有更好的建议吗?

推荐答案

303怎么样-参见其他"?似乎适合.我提请您注意这句话
摘自 https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

How about 303 - See Other? Seems to fit. I draw your attention to this sentence
from the spec at https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

此方法主要用于允许POST激活的脚本的输出将用户代理重定向到选定的资源.

This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource.

这听起来像是您想对我做的事情.剩下的就是这个.

That sounds like what you want to do to me. Here's the rest of it.

10.3.4 303查看其他

10.3.4 303 See Other

可以在不同的URI下找到对请求的响应,并且应该使用该资源上的GET方法来检索该请求.存在此方法主要是为了允许POST激活的脚本的输出将用户代理重定向到选定的资源.新URI不能替代原始请求的资源.303响应一定不能被缓存,但是对第二个(重定向的)请求的响应可能是可缓存的.

The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource. The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.

应在响应中的位置"字段中指定不同的URI.除非请求方法是HEAD,否则响应的实体应包含简短的超文本注释,并带有指向新URI的超链接.

The different URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

  Note: Many pre-HTTP/1.1 user agents do not understand the 303
  status. When interoperability with such clients is a concern, the
  302 status code may be used instead, since most user agents react
  to a 302 response as described here for 303

这篇关于如果POST成功但未创建任何新内容,我们应该返回什么状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆