3 ms·
Aside from my HTTP/REST rant, I think there's a much better discerning factor between POST and PUT. From the HTTP spec: "The fundamental difference between the
by aGHz 15y ago
Aside from my HTTP/REST rant, I think there's a much better discerning factor between POST and PUT. From the HTTP spec:
"The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. [...] In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI, it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request."
So if you want to create the user Foo and you somehow know about the URI example.com/users/Foo, then you can directly PUT your information to that URI. This URI points directly to the enclosed entity, the user. If you only know the URI of the enclosing resource (aka collection), then you MUST do a POST to example.com/users/.
So that's the semantics of it, PUT deals directly with the resource that the URI points to, while POST deals with a collection of subordinate resources.
However, the REST catch is that if you follow the HATEOAS principle, there's (almost+) no way you would know about example.com/users/Foo. In the REST world, you would (almost) never use PUT for creating, because if that resource doesn't already exist, you would (almost) never get a URI to it by traversing the hypertext representation of the application state.
+ I say almost because you could have something like a GET example.com/users?name=Foo that would return 404 with the URL where to PUT to create this user. Not sure how HATEOAS proposes you get to a URL like example.com/users?name=Foo though, perhaps someone more knowledgeable can pitch in.
- o1iver 15y agoI am not sure about this, but I think that if you adhere to the HATEOAS principle you could provide URIs for non-existent resources that can then be used to PUT. Example a user profile may contain a URI to the user's profile picture although that does not exist yet, which means that you could grab that URI and then PUT a picture there.
- aGHz 15y agoActually you're quite right, yeah. My feeling though is that in cases like this it doesn't really matter what the PUT really does behind the scene. For your API consumer it just changes the user's picture (in some cases from None to one). So yeah, PUT can both create and update in this case, but the consumer has no need to make the distinction between the two semantics.
- arethuza 15y agoIn a RESTful API that I'm currently working on I did wonder about how best to create URIs for resources that you want to create via a PUT. To try to stick to the rule that "Servers must have the freedom to control their own namespace" (which is sensible if you are going to have multiple difference server implementations with different technology stacks) I chose to return a template URI for the container resource and then allow the client to use this template to create URIs. Along the lines of: http://foo/bar/{name} http://foo/bar/{name} or http://foo/floop.ashx?path=/bar/{name} http://foo/floop.ashx?path=/bar/{name} The client replacing {name} with the actual intended resource name. Not sure if this is a good idea or not - but it seems to work!