S3 REST API和POST方法方法、REST、POST、API

2023-09-11 08:09:13 作者:半成烟沙忆空城

我使用 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不充分休息兼容?

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.

尽管流行的用法和wides $ P $垫的误解,包括高评级的答案在这里对堆栈溢出,POST不是正确的方法来创建资源。 POST是用于不是由HTTP规范任何操作的方法,因此它可以用于创建,但也可用于更新,或其他任何尚未完成通过某些其它方法,由定义目标媒介类型本身。例如,它是错误的使用POST进行检索,因为你们有标准化的,但它的优良使用POST创建资源时,客户端只有一个不完整的重新presentation(although我对那个怀疑)。

Despite the popular usage and widespread misconception, including high-rated answers here on Stack Overflow, POST is not the "correct method for creating resource". 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 and is defined by the target media type itself. 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 has only an incomplete representation (although I have doubts on that).

在以相同的方式,PUT不是正确的方法来更新资源。 PUT是​​用来完全替换资源,忽略所述资源的当前状态的方法。您可以使用PUT创造,如果您有全重presentation服务器期望,并且可以使用PUT用于更新,如果你提供的所有领域,包括那些你将不会改变,但它是不正确的使用PUT对于部分更新,因为你问的服务器要考虑资源的当前状态。补丁是做到这一点的方法。

In the same way, PUT is not the "correct method for updating resource". PUT is the method used to replace a resource completely, ignoring the current state of the resource. 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 all fields, including those 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:

发表:借此数据,并将其应用到确定由给定的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 completely 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你有一个更新的条件你想从更新状态,与把你替换整个实体更新,所以它是一个幂等操作,并与POST你问的服务器做根据predefined规则吧。

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是一种架构的风格的,而不是一个规范或标准,但即使考虑到,只有极少数的API声称自己是谁其余的都是真正的平安,在大多数情况下,因为他们没有的超文本驱动。 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.