Sacrificial RabbitCode.  Art.  Perversion.  Madness.

Tuesday, May 17. 2005

PUT vs. POST


Trackbacks

AJAX and REST, 2 great tastes, that taste great together.
Two great tastes...And by Ajax, I am not referring to a minty coloured and burning flavoured scouring powder, but instead some geek-shit. AJAX is deceptively simple. It stands for Asynchronous Javascript and XML. The basic theory is that you can use
Weblog: Sacrifical Rabbit
Tracked: May 17, 15:30
(REST != CRUD)!
That is to say, REST does not equal CRUD, dammit! Sadly, even I made the flawed analogy of Rest and CRUD. It is an excusable one, because it is so easy to map POST to create, and PUT to update, but this misses the deeper semantics of the HTTP verbs, a
Weblog: Sacrificial Rabbit
Tracked: Mar 07, 09:12
PUT vs POST - Confusion Abounds
In my research on REST this last week, I’ve seen several comments noting confusion on whether to use PUT or POST. After reading several references (Elliotte Rusty Harold, Sacrificial Rabbit, and Mark Baker) , I loosely paraphrase the rule as: Us...
Weblog: License 2 Code
Tracked: Nov 04, 10:26

Comments
Display comments as (Linear | Threaded)

So, you would POST to /user/new to create a new user.

Interesting. The way I've been thinking about POST, I would POST to "/user" to create a new user. "/user/new" is a non-resource, it doesn't exist. That isn't to say that a REST app shouldn't expose pseudo-resources, I'm not sure about that -- but I think POST is intended to be sent to the container/parent resource directly.
#1 Avi Flax (Link) on 2006-03-09 09:24 (Reply)
You are of course correct. A POST to /user to create a user, versus a post to /user/new.

Looks like I need to update this entry.
#1.1 Jonnay on 2006-03-09 09:51 (Reply)
Cool, 1 gold star for me!

Another note: in the REST app I'm working on, I use a plural name to denote a collection. Examples:

http://system.com/users/avi
http://system.com/log/events/345566
#2 Avi Flax (Link) on 2006-03-09 09:54 (Reply)
Thanks for catching that BTW... ;)

plural names as a collection totally makes sense. I'm still working out the implications of decoupling the resources from the filesystem. On one hand when the URI is not related to the filesystem at all, you get a tremendous amount of flexibility.

On the other hand, I think that the filesystem is a really good mapping between a URI and the "physical" resource on the server. I.e. if I am referring to the users resource on system.com, then it is the resourse emitted by /DOCUMENT_ROOT/users.(php|cfm|jsp)

It makes understanding a website and is associated resources much easier (in my eyes)

But like I said, I am still working through all the implications.
#2.1 Jonnay on 2006-03-09 10:04 (Reply)
Jonnay, I totally agree that a direct filesystem->URL structure mapping is simple, convenient, and effective -- for websites.

However, the definition of "website" is evolving, or fragmenting, or something -- I don't know! As I see it, the WWW was originally a document publishing system. Then it sprouted a new type of software platform -- which was shoehorned onto a publishing system. So then a website was either a collection of documents or a software application structured like a collection of documents - or both at the same time.

So it's only natural that we have a lot of uncertainty and befuddlement about what the hell is going on.

I see the development of REST applications as a clear way to use the WWW as a software platform, distinct from a website comprised of documents.

With these new REST applications, which really aren't "websites" as we know them -- although they can contain and serve documents, the application is more than a collection of documents -- I've found that the best thing we can do to fully realize their potential is to decouple them from our internal conceptual model of a "website" and think of them as a new kind of entity.

So, now we have this new entity: a REST application. It can be thought of as a tree/collection of resources contained in a single root resource, each of which can contain child resources of their own. As you know, a URL structure can be mapped the same way, as can a filesystem. So what's the problem?

Methods!

In a REST application, as you know, most resources will support 2-4 of the "major" HTTP methods. For instance, "/users" will probably support GET and POST. "/users/avi" will probably support GET, PUT, and DELETE, and maybe POST too.

OK, so what? Couldn't we have a single file for each resource, which would be coded to detect which method was specified, and take the proper action and return the proper response?

Sure we could. But I see two problems with that approach:

(1) This is a little too similar to a conventional website. That's fine for a conventional website, but when constructing a REST application, it's important that we disassociate it from our conceptual model of what a website is. When we see users.php and user.php, it will line up with our conceptual model of a website, where most resources are intented to support only GET, with a few supporting POST. This may seem silly, but I think it's the only way for us to realize the full potential of this new application architecture without being hampered by past conventions.

(2)There's a strong possibility of a lot of code duplication happening, without much reuse. I suppose this could be avoided by encapsulating the behavior in classes, but there are still a lot of files to deal with, and a lot of code scattered around. It just feels a bit messy to me.


... so, in my current project, I've abstracted resources into application concepts that a central request handler processes and returns the appropriate response/representation.

I suppose there could be a middle ground, though: some fancy HTACCESS mod_rewrite magic could enable a structure like this:


/wwwroot/resources/users/get.php
/wwwroot/resources/users/post.php
/wwwroot/resources/users/user/get.php
/wwwroot/resources/users/user/put.php
/wwwroot/resources/users/user/delete.php


and so on. Again there are a lot of files and shared libraries would be necessary to reduce code duplication, but it would probably work fairly well. However, I personally wouldn't want to have the job of writing all those mod_rewrite rules.


Whew, I think that's quite enough for now! I am *done*!

Please forgive me if this seems like lecturing or something, really I'm just thinking out l... ah, through my fingers. Thanks for letting me blather on!

(BTW why not allow some HTML in the comments?)
#3 Avi Flax (Link) on 2006-03-10 07:20 (Reply)

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA