views:

422

answers:

2

I have a URI for a collection of resources called 'facts', and URIs for each 'fact' resource in that collection.

The form for creating a new 'fact' should be requested with a GET, I believe, but I'm having trouble deciding what URI it should be made to.

A GET to the collection URI should return a list of the 'fact' resource URIs. Each 'fact' URI should return its contents as a response to GET. The actual 'fact' creation would be a POST (or PUT, depending on the situation), of course.

I see a few options, but none seem satisfactory:

  1. Add a 'fact form' URI which the 'facts' URI will reference. A GET to this URI gives the HTML form. Seems wrong to have another resource just for a description of a resource.
  2. A POST made to the 'facts' URI without including any form data in the headers would return the form. Then after the user fills the form in, it would POST with the form data, and create the new 'fact' resource. This seems like an even worse approach.
  3. Don't send the form over the wire, but include it as part of the API. This seems RESTful since a REST API should describe the media types, and a form can be made from a description of the 'fact' type. This is weird to implement. Maybe the REST service is separate from the regular web site, so that the actual HTML form request is at some URI apart from the REST API.
  4. Include the HTML form as part of the 'facts' URI response.

To clarify, I'm trying to follow true REST architecture as specified by Roy Fielding, not half-baked RPC posing as REST.

edit: I'm starting to think #3 is on to something.

edit2: I think a solution is to have regular non-REST HTML navigation in a CRUD manner, and then the frontend makes AJAX REST calls as appropriate (or the backend makes internal calls to its REST API).

The reason I need to do the REST part of this service correctly is that I want to allow other non-HTML clients to interact with it later on.

A: 

I think you're overcomplicating things a bit. A web browser is just not a perfect REST client, so you can't have a perfectly RESTful solution. In a perfect world, you would not need a form at all, because the web browser would know your media types and build the form itself.

Meanwhile, I suggest you just use what most REST frameworks would call an additional "view" on the resource to return a form:
E.g. /your/collectionresource?view=form, or /your/collectionresource;form

trendels
The latter is what I suggested in 1 - an entire different URI for the form. I don't care what the URI itself looks like, that's irrelevant to REST. The former is misusing a query, but might have some merit.
Wahnfrieden
What do you mean with "misusing a query"? Is there some drawback to this approach? I'd like to know because this is what I would probably have used in this situation.
trendels
Queries are supposed to be for requesting a subset of some set of resources, like an actual query - not for specifying a specific resource.
Wahnfrieden
I agree that a web browser is not a perfect REST client, but I disagree that you cannot have a perfectly RESTful solution using one. The REST style was extracted from the implementation of the web using web browsers, so by definition, it must be possible.
Darrel Miller
+1  A: 

In my mind, the only cleanly RESTful answers are 1 and 3.

As I see it, the description of the resource is a resource of its own. The question is whether you want to make this resource accessible through your application's API or if you want to make it part of the API itself.

For 1, it seems RESTful make the URIs something like this:

GET /facts -> all facts GET /facts/1 -> returns fact 1 (obviously the id might be a word or something else) GET /facts/create -> returns a form appropriate for creating a fact POST /facts -> adds a fact

Chuck
But descriptions are resources belong in the API. So it doesn't seem RESTful to send them over the wire.
Wahnfrieden
I think I need to separate the REST interface from the HTML interface. The HTML interface makes calls to the REST URIs, whether from the frontend via AJAX, or just internally on the server. And the REST interface knows nothing about the HTML interface.
Wahnfrieden
I don't think you can categorically say "descriptions belong in the API." This sort of thing is a design choice. Sometimes, the description of a resource might not be completely known at the time you're writing the API — take a look at CouchDB for an example of something like this. In your case, I agree that the API is the best place for the description, and your HTML frontend can then make use of that API.
Chuck
@Chuck according to REST, the media type description belongs in the API. I guess what can happen is you can have a media type that is a wrapper some other kind of description, but that is apart from REST, in my interpretation of it.
Wahnfrieden
The fact that HTML defines the FORM element which allows a client to dynamically construct an entity is a perfectly valid thing for a media type to do. I don't see it violating any RESTful constraints.
Darrel Miller
I agree with Chuck that 1 and 3 are the best options. Without knowing the complete scope of your requirements it is tricky to know which is best in your scenario. Regarding solution 1, when you respect the uniform interface constraint, you are sometimes going to have to create resources that are bit unnatural. However, those resources are discoverable via hyperlinks, so really whether the client does GET /Facts/Create or GET /Facts is not relevant.
Darrel Miller