72 points · by jedwards_tech · 14y ago · 34 comments
"The model application is therefore an engine that moves from one state to the next by examining and choosing from among the alternative state transitions in the current set of representations."
http://weblogs.java.net/blog/mkarg/archive/2010/02/14/what-h...
"But honestly, why would your users be creating an object with an id?" They aren't. You should not be PUTting endpoints with an id. I get more more and more scared as I type, thinking about this change-the-standard-for-my-own-purpose attitude.
There are about a million questions you must ask yourself in order to come to the right answer for YOU. At the end of the day what matters is that the person using your API finds it easy and intuitive. If that means that you use PUT for updates, and POST for creation, so be it. It could even mean that you convert PUT to POST requests, and treat them all the same. If you take that route, you check to see if the resource ID is specified in the post data. If the ID is in there, you update that resource, if it's not in there you create a new resource.
While you go forward, keep in mind one thing. If your API is hard to use then no one will use it. If no one uses it, then your API doesn't matter. If your API doesn't matter, then you made all the wrong decisions.
Also, have fun with it. Creating APIs is incredibly fun, enjoy yourself, and enjoy creating something people will enjoy using.
Replacing the nonsense of SOAP and XML-RPC with equally ridiculous pedantry about HTTP minutiae is not helping your cause nor your software.
edit: Well, ajross has a point. There are actually all of eight HTTP 1.1 methods, and PATCH is not one of them.
It is "easy" to blindly follow any rule. In most cases it is also wrong.
Having said that, I like subresource URLs and using PUT to do partial updates like that.
1. 400 bad data - a failed request
2. Blank out / delete the values of all fields not specified
3. Perform a PATCH-style partial update
Which of these is the most useful / least surprising / closest to the spec?
Now read the spec: http://tools.ietf.org/html/rfc2616#section-9.6 PUT should store the request as the resource, it doesn't have to. Also note the complete absence of push from the precious specification! (I know patch is an amendment, my point us that the HTTP spec shouldn't be considered perfect)
I'm doing this for the API I'm building, as modeled on FatSecret's API.