Resources can be represented in multiple formats - JSON, XML, Atom, Binary formats like png, text and even proprietary formats. If client request a resource the REST service transfers the state of a resource (and not the resource itself) in the appropriate format.
Assume that you are designing RESTful interface for providing metadata for Cars and your service gets consumed by many clients, some traditionals as well as few startups. So each one of them have their own requirements to provide response in the given format. Let's see what are available options-
Approach 1: One URI per representation
http://www.myservice.com/carshttp://www.myservice.com/cars/xml
The first URI is default representation of the resource and second one returns the response in xml format. Both URI are different so there will be different handlers (end point) and hence the response can be easily returned in appropriate format.
Approach 2: Use Parameter of URI
http://www.myservice.com/cars?format=xmlThis approach is easy to read and understand.
Approach 3: Single URI for all representation
This approach comes from the fact that if client is essentially asking for the same resource then why do we need different URIs. Remember, REST uses HTTP; can we leverage HTTP ACCEPT header to get different representation of the same resource. This is process of selecting the best representation for a given resource- termed as Content Negotiation.
Content Types
HTTP uses Internet media types (originally known as MIME types) in the content-type and accept header fields. Internet media types are divided into 5 top level categories: text, image, audio, video and application. And then these types are further divided into several subtypes:- text/xml : default content type for text message
- text/html : commonly used type used in browsers
- text/xml, application/xml: Format used for xml exchanges
- image/gif, image/jpeg, image/png: image types
- application/json: language independent light weight data-interchange text format
GET /cars/
Accept: application/json
So respect the HTTP headers and everything works out.
This approach could be bit code intensive for some frameworks like Django as you need to dig into headers and decode what clients wants. But most of the Java frameworks handle it though annotation.
Final Note
No matter which approach you use. It would be great if you stick to one across the services. Prefer to be consistent even if that leads to not being very right!
References
No comments:
Post a Comment