tags:

views:

402

answers:

4

Hi,

I'm developing a REST API and I have a question about resource representations.

Suppose I got the "person" resource under the /app/person/{id} URI. I need an XML representation, that basically is all the object fields as XML nodes under the root. Now requirements indicate that we must also support another kind of XML representation enforced by a proprietary schema.

The question is: is it under REST best practices to support a proprietary content type like "text/my-type" for the same resource? note that both are XML but formatted differently, and most important they don't carry the same information (eg. one representation may include other fields like "modified-since")

Important!: I know that being pragmatic and keeping it simple it's more important than guides and "best practices" but I just wanted to know if that's the way to go under a RESTful architecture.

+1  A: 

If these are just two different representations of a Person resource, then you ought to have two media types for them. If at all possible try to find and reuse standard representations and their media types (see http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven). Both media types should have the form application/type+xml (and see Darrel's comment).

Jim Ferrans
Custom types based on xml are named application/type+xml. If you are creating your own custom type then usually they are created under the vendor subtree. e.g. application/vnd.mycompany.mytype+xml
Darrel Miller
Thanks Darrel, I've flipped "xml" and "type".
Jim Ferrans
Thanks Jim, link is broken though
Pablo Fernandez
Sorry Pablo, corrected link. It wasn't my day!
Jim Ferrans
@Darrel I was wondering why xml doesn't come before the vendor tree - application/xml+vnd.mycompany.mytype ? Then clients which handle any type of XML, even just a simple XML prettifier, could easily see that it is an application media type, and using XML, and then it further specializes into a vendor specific space. This makes more sense to me, unless the content-type specifications instruct clients to ignore what comes between the / and + if they don't care about vendor specific specializations or something wacky like that.
Wahnfrieden
@Wahnfrieden See here http://tools.ietf.org/html/rfc3023#page-32
Darrel Miller
@Darrel perfect, thank you.
Wahnfrieden
+3  A: 

Yes and no. There are no REST constraints that prevent you from returning two different representations of a resource from the same URL. Even, if one media type is a proprietary format. Be careful about allowing the content to vary too much, I hear that some people get pretty upset about that. Also, for the custom formats you should use a media type under the vendor subtree

e.g. application/vnd.companyname.format+xml

However, it is not really in the spirit of REST to return proprietary formats. That being said, you can do with without any problems other than limiting serendipitous re-use.

Darrel Miller
REST has no issues with proprietary formats - HATEOS actually describes this scenario perfectly. A client who wants to interact with a RESTful service need only know the media types returned by that service (whether they be jpeg, Atom XML, Word docs, etc...)
Gandalf
A: 

Content Negotiation is built into HTTP using the Accept and Accept-Encoding headers. The client apps should specify what type they want returned by setting these headers.

Gandalf
This does not answer what I asked... seems like a comment more than an answer
Pablo Fernandez
+1  A: 

If the second format is simply a different syntax (or can reasonably be viewed as such), it's perfectly fine (and RESTful and in compliance with REST best practices) to add it as a second representation with another media type. If you consider the difference to be more than syntactical, you should probably create a different resource. The same is true if you want to be able to link to a specific representation (because it needs a different URI if you want to do so). In this latter case, you might want to consider a canonical, format-independent resource as well that can return a 303 See Other with links to the specific ones.

Stefan Tilkov