Regarding the actual title of the question I somehow disagree with the accepted answer given by @TatsuyukiIshi. PUT
s 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).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…