S3 REST API 和 POST 方法 [英] S3 REST API and POST method

查看:33
本文介绍了S3 REST API 和 POST 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AWS S3 REST API,并且在解决了一些烦人的签名问题后,它似乎有效.但是,当我使用正确的 REST 动词来创建资源时,即 POST,我得到 405 方法不允许.相同的请求与方法 PUT 一起工作正常并创建资源.

I'm using AWS S3 REST API, and after solving some annoying problems with signing it seems to work. However, when I use correct REST verb for creating resource, namely POST, I get 405 method not allowed. Same request works fine with method PUT and creates resource.

我做错了什么,还是 AWS S3 REST API 不完全符合 REST 标准?

Am I doing something wrong, or is AWS S3 REST API not fully REST-compliant?

推荐答案

是的,您将 CRUD 映射到 HTTP 方法是错误的.

Yes, you are wrong in mapping CRUD to HTTP methods.

尽管使用广泛且误解广泛,包括 Stack Overflow 上的高评价答案,但 POST 并不是创建资源的正确方法".其他方法的语义由 HTTP 协议决定,而 POST 的语义由目标媒体类型本身决定.POST 是用于任何未由 HTTP 标准化的操作的方法,因此它可用于创建,但也可用于更新,或其他方法尚未完成的任何其他操作.例如,使用 POST 进行检索是错误的,因为您已经为此标准化了 GET,但是当客户端由于某种原因无法使用 PUT 时,使用 POST 来创建资源是可以的.

Despite the popular usage and widespread misconception, including high-rated answers here on Stack Overflow, POST is not the "correct method for creating resource". The semantics of other methods are determined by the HTTP protocol, but the semantics of POST are determined by the target media type itself. POST is the method used for any operation that isn't standardized by HTTP, so it can be used for creation, but also can be used for updates, or anything else that isn't already done by some other method. For instance, it's wrong to use POST for retrieval, since you have GET standardized for that, but it's fine to use POST for creating a resource when the client can't use PUT for some reason.

同样,PUT 也不是更新资源的正确方法".PUT 是用于完全替换资源的方法,忽略其当前状态.如果您拥有服务器期望的整个表示,则可以使用 PUT 进行创建,如果您提供完整表示(包括您不会更改的部分),则可以使用 PUT 进行更新,但使用 PUT 进行部分更新是不正确的,因为您要求服务器考虑资源的当前状态.PATCH 就是这样做的方法.

In the same way, PUT is not the "correct method for updating resource". PUT is the method used to replace a resource completely, ignoring its current state. You can use PUT for creation if you have the whole representation the server expects, and you can use PUT for update if you provide a full representation, including the parts that you won't change, but it's not correct to use PUT for partial updates, because you're asking for the server to consider the current state of the resource. PATCH is the method to do that.

在非正式语言中,每个方法对服务器说的是:

In informal language, what each method says to the server is:

  • POST:按照您为资源媒体类型记录的规则,获取此数据并将其应用于由给定 URI 标识的资源.

  • POST: take this data and apply it to the resource identified by the given URI, following the rules you documented for the resource media type.

PUT:用此数据替换给定 URI 标识的任何内容,忽略已经存在的任何内容(如果有).

PUT: replace whatever is identified by the given URI with this data, ignoring whatever is in there already, if anything.

PATCH:如果由给定 URI 标识的资源仍然具有我上次查看时的状态,则应用此差异.

PATCH: if the resource identified by the given URI still has the same state it had the last time I looked, apply this diff to it.

注意没有提到创建或更新,也不是这些方法的语义的一部分.您可以使用 POST 和 PUT 创建,但不能使用 PATCH 创建,因为它取决于当前状态.您可以使用其中任何一个进行更新,但是使用 PATCH 可以根据要更新的状态进行更新,使用 PUT 可以通过替换整个实体进行更新,因此这是一个幂等操作,使用 POST 可以要求服务器执行它根据预定义的规则.

Notice that create or update isn't mentioned and isn't part of the semantics of those methods. You can create with POST and PUT, but not PATCH, since it depends on a current state. You can update with any of them, but with PATCH you have an update conditional to the state you want to update from, with PUT you update by replacing the whole entity, so it's an idempotent operation, and with POST you ask the server to do it according to predefined rules.

顺便说一句,我不知道说 API 是否符合 REST 是否有意义,因为 REST 是一种架构风格,而不是规范或标准,但即使考虑到这一点,很少有声称是 REST 的 API 是真正的 RESTful,在大多数情况下,因为它们不是 超文本驱动.AWS S3 绝对不是 RESTful,尽管它与您的问题有关,但它们对 HTTP 方法的使用大部分时间都遵循 HTTP 标准.

By the way, I don't know if it makes sense to say that an API is or isn't REST-compliant, since REST is an architectural style, not a spec or a standard, but even considering that, very few APIs who claim to be REST are really RESTful, in most cases because they are not hypertext driven. AWS S3 is definitely not RESTful, although where it bears on your question, their usage of HTTP methods follows the HTTP standard most of the time.

这篇关于S3 REST API 和 POST 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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