问题描述
我正在设计一个API REST服务,允许用户将文件上传到服务器.
我认为这是一个puct请求,它将转到服务器/资源/id,并在json请求正文中将文件作为base64.
我的问题就是这个ID.在我的头上,我将文件传递给服务器,服务器应该负责存储该文件并生成唯一的ID以稍后检索它,然后用OK状态返回该ID到客户端.
所以我正在考虑这样做,将它发送到服务器/资源,没有id,但这是好的,还是它是坏的设计?
推荐答案
no.将意味着"创建或更新",并且应该具有显式ID.帖子适合创造新的东西.
其他推荐答案
关于问题的实际标题,我以某种方式不同意@tatsuyukiishi给出的接受答案. PUT S语义是:用请求中包含的有效载荷替换当前在给定URI处获取的内容.如果可以在没有id的情况下识别资源,即,只有可能存在它的一种类型,可以在不指定ID的情况下解决更新,因为在端点本身中已经隐式给出"单例资源"的ID.虽然,我必须承认这很少是这种情况.
这种情况可能是剪贴板,如资源,您可以在其中放置任意内容并稍后检索它.当然,您也可以使用POST,但是使用POST请求接收的主体的语义不太清楚.此外,POST不符合PUT操作.
然而,使用PUT /api/messages这样的东西通常意味着替换与用请求发送的内容替换所有消息,这可能不是您真正想要的.通常,您只想一次修改单个资源,因此使用标识特定资源的附带ID. 关于问题的实际内容,通过POST上传文件是常识.在成功上传时,您将返回包含指向生成的资源的Location http标头的201 Created响应.通过POST请求接收的服务处理内容的行为总计到服务实现者.因此,您可以创建一个新的资源,在没有任何实际资源创建的情况下执行一些备份任务,或者其他(规范不禁止更新).其他推荐答案
为时已晚,但我在同一个问题上发现了很多错误的信息,所以我会让这里找到的东西.
有2条RFC规则依赖,这个问题的一个是RFC 7231,在那里你会发现:
PUT方法请求目标资源的状态 创建或替换为表示所定义的状态 括在请求消息有效载荷中.
所以你不能在没有身份证的情况下发送放置.
很多Restful API即使在更新时也会发出一篇文章,这也是错误的RFC,所以你应该始终发送ID来用PUT创建它,或者应该使用帖子来创建和更新,但请记住帖子应始终在另一个单词中创建,如果您不先使用Get,则会复制您的文件.
问题描述
I'm designing an API Rest service that allows the user to upload a file to the server.
I'm thinking this is a PUT request and it would go to server/resource/ID and have the file as base64 in the json request body.
My question is regarding this ID. In my head, I'm passing the file to the server and the server should be in charge of storing that file and generating a unique ID to retrieve it later, and then return this ID to the client with an ok status.
So I'm thinking about doing that, sending it to server/resource, without the ID, but is this ok or is it bad design?
推荐答案
No. PUT means "create or update", and should come with an explicit ID. POST is suitable for creating something new.
See also: PUT vs POST in REST
其他推荐答案
Regarding the actual title of the question I somehow disagree with the accepted answer given by @TatsuyukiIshi. PUTs semantics are: Replace the content currently obtainable at the given URI with the payload contained in the request. If a resource can be identified without an ID, i.e. there only ever may exist one of its kind, it IS possible to address an update without specifying an ID as the ID of the "singleton resource" is already implicitly given in the endpoint itself. Though, I have to admit that this is rarely the case.
Such a case may be a clipboard like resource where you can put arbitrary content to and retrieve it later on. Sure, you could also use POST, though the semantics of the body received with the POST request are less clear. Also POST is not idempotent in contrary to PUT operations.
Using something like PUT /api/messages, however, would usually mean replace all messages with the content sent with the request which might not be what you really want. Usually you only want to modify a single resource at once and hence use an accompanying ID that identifies that specific resource.
In regards to the actual content of the question, uploading a file via POST is the common practice. On a successful upload you will return a 201 Created response that contains a Location HTTP header that points to the generated resource. The behavior of a service processing content received via POST requests is totaly up to the service implementor. Therefore you could create a new resource, perform some backing task without any actual resource creation or something other (even updating is not forbidden by the specification).
其他推荐答案
Too late for you, but I was with this same question and found a lot of wrong information, so I will let here what a found.
There are 2 RFC that rules RESTful, the one about this question is RFC 7231, in that you will find:
The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload.
So you can't send a PUT without the ID.
A lot of RESTful API send a POST even on updates, that's wrong too by that same RFC, so you should always send the ID to create it with PUT, or should use POST to create and PUT to update, but remember that POST should always create, in another words, you will duplicate your file if you don't look for it first with GET.
For more information: https://www.rfc-editor.org/rfc/rfc7231#section-4.3.3