28

I'm writing a paper about implementing a REST service for a university's research papers and I have a small problem understanding the relationship between URIs and Resources.

It says, that a resource may have one URI or many. So here is my problem. I want to make this service very easy to use and to get around the information: a resource should be accessed from different entrypoints, but this will go against the concept, that every "URI designates exactly one resource".

So my question is if the following is it or is it not in accordance with REST the following:

I want to expose information about a research publication (let's say a peer reviewed).

This can be accessed by this URI: UNIVERSITY/publications/{my_publication}.

But since this paper is written by a researcher that works at the let's say Social Science Faculty, it will also make sense that the publication has this URI: UNIVERSITY/faculties/social_science/publications/{my_publication}.

Further more, as the service also expose all researchers working at the university (e.g. UNIVERSITY/researchers/{my_researcher}) it will also make sense that the publication can be named as UNIVERSITY/researchers/{my_researcher}/publications/{my_publication}.

This could go on with multiple usecases, but you get the idea.

Is this in accordance with REST or not?

Can I keep this and solve the dilemma by sending a response code 303("See also") along with the canonical URI (that will be UNIVERSITY/publications/{my_publication}).

Thank you in advance!

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83

6 Answers6

29

It says, that a resource may have one URI or many. So here is my problem. I want to make this service very easy to use and to get around the information: a resource should be accessed from different entrypoints, but this will go against the concept, that every "URI designates exactly one resource"

No it doesn't. "Every finger belongs to exactly one hand" does not imply "every hand has exactly one finger". "Every URI designates exactly one resource" does not imply "every resource is designated by exactly one URI".

That said, a redirect to a canonical URI would improve some use cases ( such a two users bookmarking the same paper on delicious, having arrived there from different queries ).

You also seem to be thinking about constructing URLs via hierarchic patterns, rather than anything about REST. REST applications use "hypertext as the engine of application state". The form of the URI is irrelevant, what matters is that it is navigable from the representation returned at the application's entry point. Fielding (the inventor of REST) makes this clear in his blog REST APIs must be hypertext-driven as an anti-pattern causing coupling between client and server:

A REST API must not define fixed resource names or hierarchies (an obvious coupling of client and server).

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • 3
    Boy would I like you to expand on that last paragraph a bit. – ScottCher May 01 '12 at 21:47
  • 3
    @ScottCher In a REST application, the state of the application is represented by the user agent fetching resources via URLs. REST is thinking of an application as a state machine where each state is the result of resolving a URL to retrieve a resource; the links in the resource are the transitions to the next possible states. Having the links have a particular human readable pattern is not relevant to REST itself, only that the links to the next state of the application are inside the resource retrieved for the current state. Having the same state represented by the same URL facilitates ... – Pete Kirkham May 02 '12 at 12:23
  • 2
    ... bookmarking, but having a pattern that users can guess is somewhat anti-REST - it means a user can guess a URL, type it in and jump into an application state that wasn't part of their workflow. For a lot of cases, this isn't a problem, where the state is not dependant on having been reached from a given route, so quite a lot of REST applications allow it. Having a nice human readable pattern has advantages, but is not a requirement of REST. – Pete Kirkham May 02 '12 at 12:26
  • 2
    Interesting - there is quite a lot of content out there that goes the other way, recommending that URI's should be hackable up and down. Some people taking that notion perhaps a bit further than others, like http://redrata.com/restful-uri-design/ . Thanks for clarifying. – ScottCher May 03 '12 at 12:55
  • Too bad the OP is no longer a SO user, this response deserves to be checked as the answer! – Dave Jul 05 '12 at 22:23
  • What would happen if you make a `PUT` request to the "non-canonical" endpoint specifying the non-canonical identifier? Normally, a PUT request would create the resource if the client specifies the full resource uri and the server can honor that as the final "resource ID", but in this case, we would be posting to what is usually an alias endpoint. Would that be allowed at all and still be restful? – julealgon Aug 01 '22 at 18:26
15

This is a frequently debated topic and I believe the confusion is based on trying to understand what does an HTTP URI actually point to. Based on my readings this becomes a really hairy topic and people way smarter than myself debated for many years on the subject.

Here is the a summary page of all of the discussion on the http-range-14 issue.

My naive interpretation of the final conclusion of this issue is that there should only be a single URI that returns a physical "information resource" with a 200. However, there can be many URIs that refer to the resource as a pure concept. Returning 303 allows you to link the concept to "information resource".

So the answer is yes and no, there can be multiple URIs for the same resource and all are valid for representing the concept, but only one should actually return the physical representation.

This is consistent with a recent comment from Roy Fielding when talking about using ".xml" and ".json" in URIs. He stated quite clearly that http://www.example.org/myresource.xml and http://www.example.org/myresource.json are refering to two different RESOURCES because they are both returning a 200. However, when you use content negotiation on http://www.example.org/myresource you could retrieve two different representations of the same resource.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
10

While each publication resource should have one and only one resource name (URI), you're free to create resources that are queries and that return lists of other resource names.

You could have UNIVERSITY/publication/{publication} as the resource name pattern for publication resources, and UNIVERSITY/faculties/{faculty}/publications as the pattern for naming resources that are lists of publications for particular faculties. Likewise UNIVERSITY/researchers/{researcher}/publications could be the resource name pattern for the lists of publications authored by a particular person.

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
  • Hi Jim, that's exactly what I did. I even went further, but also naming sresource for each institutes publications (/faculty/institute/publications) and so on. That's not my dilemma, but tha fact that no matter what URI I have in front of {my_publication}, the URI points on one and the same ressource: the publication itself, which is unique. –  Dec 18 '09 at 16:58
  • @jan, Not sure if this addresses your comment, but each query can be a REST resource just like each publication is. A query properly should contain just a list of publication *names*. So a client of your web service might first get the list of all the publications from the USC Information Sciences Institute, and then get each publication in that list. – Jim Ferrans Dec 18 '09 at 17:10
1

It says, that a resource may have one URI or many. So here is my problem. I want to make this service very easy to use and to get around the information: a resource should be accessed from different entrypoints, but this will go against the concept, that every "URI designates exactly one resource".

I don't think there's any contradiction here. A URI can point to at most one resource, but many URIs can point to the same resource. A many-to-one relationship between URIs and resources, if you will.

That said, I wouldn't freak out too much over whether your application is RESTful or not. It's just a design principle. Here is a nice article by a guy who argues that REST isn't really intended for humans with web browsers anyway: http://starkravingcoder.blogspot.com/2009/01/where-are-rest-frameworks.html

danben
  • 80,905
  • 18
  • 123
  • 145
1

+1 to Jim Ferrans.

Also, having exactly one URI per resource will make it easier to create links within your site. I have read that search engines prefer content not to be repeated at different URIs too.

Aaron Lockey
  • 830
  • 1
  • 8
  • 15
  • That's true, and another advantage is that if you only have a single name per resource, then it's much more cachable than if you have a bunch of synonyms. – Jim Ferrans Dec 18 '09 at 17:03
0

You need to see the difference between a resource and the entity it represents. Roy Fielding writes in his dissertation, section 5.2.1.1:

A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time.

Since all of your resources carry slightly different semantics, it can be considered RESTful it my opinion. Depending on the structure of your media type you could use the canonical link relation to indicate the "preferred uri".

Community
  • 1
  • 1
Michael A. Schaffrath
  • 1,992
  • 1
  • 14
  • 23