views:

134

answers:

4

I'm almost afraid to post this question, there has to be an obvious answer I've overlooked, but here I go:

Context: I am creating a blog for educational purposes (want to learn python and web.py). I've decided that my blog have posts, so I've created a Post class. I've also decided that posts can be created, read, updated, or deleted (so CRUD). So in my Post class, I've created methods that respond to POST, GET, PUT, and DELETE HTTP methods). So far so good.

The current problem I'm having is a conceptual one, I know that sending a PUT HTTP message (with an edited Post) to, e.g., /post/52 should update post with id 52 with the body contents of the HTTP message.

What I do not know is how to conceptually correctly serve the (HTML) edit page.

Will doing it like this: /post/52/edit violate the idea of URI, as 'edit' is not a resource, but an action?

On the other side though, could it be considered a resource since all that URI will respond to is a GET method, that will only return an HTML page?

So my ultimate question is this: How do I serve an HTML page intended for user editing in a RESTful manner?

+1  A: 

Instead of calling it /post/52/edit, what if you called it /post/52/editor?

Now it is a resource. Dilemma averted.

Frank Krueger
It is indeed "Dilemma averted", and I thought about doing it this way. I just thought it seemed like the easiest way out, and perhaps not the conceptually right way out. I will reconsider.
tmadsen
+4  A: 

Another RESTful approach is to use the query string for modifiers: /post/52?edit=1

Also, don't get too hung up on the purity of the REST model. If your app doesn't fit neatly into the model, break the rules.

Ned Batchelder
That's what I'd recommend. It makes the most sense to me because `GET /post/52` will show you one representation (the "normal" version) of the resource and `GET /post/52?edit=1` will show you another representation (an editable form version) of the same resource.
Will McCutchen
I would suggest that a) know the rules before breaking them and b) know the consequences of breaking the rules. There are no REST rules that say what your URL should look like.
Darrel Miller
@Will Those are two different resources you are describing, not two different representations.
Darrel Miller
@Darrell, I'd have to side with Will here, a resource is not the HTML page served, it's an underlying conceptual resource. In this case, a "blog post", and whether it is for viewing or editing, it is one resource. Of course, this is all a bit subjective, and people can differ over the application of the concepts.
Ned Batchelder
@Ned You are correct, the resource is not the bytes that make up the HTML page that is served, the "resource" is the concept that is represented by the identifier `/Post/52?edit=1`. However, that is not the same resource as '/post/52'. Remember, URLs are opaque, those URLs are different and a resource should only have one URL that returns a 200 otherwise you will pollute intermediary caches.
Darrel Miller
@Ned I realize what I am saying is a bit weird. I have been struggling myself to accept this fact for a while, as it feels much more comfortable to treat a resource as equivalent to a domain entity and representations as different serializations of that domain entity. However, the more I look into this subject, the more I realize, that is not what Roy meant by resources and representations. I have been looking for the last hour for a post on REST-Discuss where Roy makes this point quite clearly but of course I can't find it at the moment. I would actually be quite happy to be wrong about this.
Darrel Miller
I like this query way of doing it, but thought that the query part of an URI were only meant to be used for input to some algorithm. I think i may have read this in "RESTful Web Services" by Richardson and Ruby, but I'm not sure. So I looked it up the RFC for URIs (3986), and it says: "The query component contains non-hierarchical data that, along withdata in the path component (Section 3.3), serves to identify aresource within the scope of the URI's scheme and naming authority (if any)." http://tools.ietf.org/html/rfc3986#section-3.4If I understand this correctly, Ned is right.
tmadsen
A: 

I don't think /object/id/action is part of the REST specification.

Is your editor going to be a generic thing for all objects ? Then maybe your URL should look like

/editor/object/id

The action is an HTTP Verb ( GET,PUT,DELETE,POST ) and is supposed to be a part of the HTTP request and not part of the URL. For a better summary, check out this Wikipedia article

http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services
George
...REST specification? :-)
admp
Please be aware that "Post(s)" is a vital part of a blog. The "post" you see in the URI is meant as a resource, not an HTTP method verb. Maybe I should have stated that more clearly in my question.
tmadsen
right .... post is the object, edit is the action
George
+2  A: 

There is no such thing as a RESTful URI. It is false concept as URIs should be completely opaque to the client.

If it helps you to properly implement the HTTP uniform interface by avoiding verbs in your URIs then that's great, but don't feel constrained by what your URI looks like. It is very limiting to think of resource modeling as type of data modelling. A RESTful system usually needs to do way more than just CRUD operations, so you need to be creative about what resources you make available in your system.

If you create a URL and dereferencing it returns a 200 status code, then that URL refers to a resource. If you create another URL and it also returns a 200, then that is a difference resource.

That means:

http://example.org/customer/10.xml
http://example.org/customer/10.json
http://example.org/customer/10?format=xml
http://example.org/customer/10?format=json

are 4 different resources, and

http://example.org/customers
http://example.org/customers?closed=true
http://example.org/customers?page=2&pagelength=20

are also different resources.

Therefore to answer your question, if you do

GET /post/52/edit 

and it returns a 200 status code and a representation, then it must be a resource.

Darrel Miller
@Darrell, you're applying the resource concept a bit unusually. If ?format=xml and ?format=json are different resources, what would you count as different representations of the same resource?From the [wikipedia page](http://en.wikipedia.org/wiki/Representational_State_Transfer): "An important concept in REST is the existence of resources... In order to manipulate these resources, components of the network ... exchange representations of these resources."
Ned Batchelder
@Ned If you look at table 5.1 in the REST dissertation you will see that URL and URN are described as RESOURCE identifiers. Different URLs point to different resources. You can use content negotiation to receive different representations from the same URL, but as soon as you give distinct URLs to those "representations" then they become resources in their own right.
Darrel Miller
@Ned Here is the email where Roy states that urls that point to an xml representation and a json representation are actually two different resources. http://tech.groups.yahoo.com/group/rest-discuss/message/13960
Darrel Miller
A representation of a resource is request in the "accept" header of the HTTP message (as per mailing list link Darrel provided). Thank you Darrel for taking the time to find that.It is good to know that "/edit/" would be a valid way of doing it, and realizing you viewpoint, that every URI that returns 200 OK is a resource, was sort of a breaktrough for me even though it should have been obvious from the start. Thank you both, Ned and Darrel, for your effots here.
tmadsen