In: Categories » Computers and technology » AJAX » Identifying the Resource and Representation using Ajax
Identifying the Resource and Representation REST promotes the separation of the resource from the representation. For illustration purposes, let’s work through the historical stock-ticker example. The URL used to retrieve the historical ticker information is /services/historical/AMZN/2006. The default format generated by the Web service is CSV, but the default could just as easily have been XML or JavaScript Object Notation (JSON). If a client can only accept JSON, then the conversion from CSV to JSON requires an extra step and extra resources.
To optimize this application, you can let the server decide which content to generate based on the needs of the client. If the client wants JSON, then the server will generate JSON. The data that is generated as JSON, XML, and CSV is all the same. Thus, it can be said that the data is the resource, and JSON, XML, and CSV are the representation. Separating the resource from the representation means that a single URL will have separate representations. The representation that is sent depends on the value of the HTTP Accept-* header, but doesn’t need to be the only one. Let’s focus on the Accept HTTP header and consider the following HTTP conversation that returns some content.
Request GET /services/historical/AMZN/2006 HTTP/1.1 Host: 192.168.1.242:8100 User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.8) Gecko/20050511 Accept: text/xml,application/xml,application/xhtml+xml,text/html; q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Response HTTP/1.1 200 OK Date: Mon, 21 Aug 2006 14:51:40 GMT Server: Apache/2.0.53 (Ubuntu) Last-Modified: Thurs, 11 May 2006 17:43:45 GMT ETag: "41419c-45-438fd340" Accept-Ranges: bytes Content-Length: 69 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/html; charset=UTF-8
The request is an HTTP GET, which means the HTTP server needs to retrieve the data associated with the resource. The operation becomes specific when the request provides the HTTP headers Accept, Accept-Language, Accept-Encoding, and Accept-Charset. These HTTP headers are accepted by the HTTP server and serve as an indication of what content to send. Focusing on the HTTP header Accept, the values are a series of MIME-encoded identifiers that the client will accept.
The order and type of the identifier are important, because they specify the priority of the content that the client wants to receive from the server. The logic is to send the content available with the priority defined by the client that, for example, forces the server to send HTML content before plain-text content. The priority of content is the priority of the MIME types as defined in the HTTP specification. The following list is generated when you reorder the example request:
1. application/xhtml+xml 2. text/xml 3. application/xml 4. image/png 5. text/html;q=0.9 6. text/plain;q=0.8 7. */*;q=0.5
The ordering of the identifiers depends on the identifier specialization and its q value. A MIME type identifier that has no q value indicates a default value of 1.0. When a q value exists, you must lower the priority of the MIME type identifier to the value specified by the q value. Identifier specialization is when one identifier is a higher priority because the specified content is more specific than the other identifier. In the list of priorities, the identifier text/xml is more specific than */* because */* means everything. Additionally, text/xml is more specific than text/*, and hence text/xml is a higher priority.
Note that the first MIME identifier from the HTTP conversation is text/xml, and the second is application/xml. Yet, in the priority order, the first MIME identifier is application/xhtml+xml. I made this assumption after having read the HTTP and MIME specifications, but I feel it’s a bug that just happened to work.
Let’s dissect the example request to understand why this bug happened to work. The MIME type identifiers application/xml, text/xml, and application/xhtml-xml are considered specific, and each has a q value of 1. If the server follows the ordering of the MIME types, it means that the browser prefers receiving XML content to HTML or XHTML content. The application/xml and text/xml MIME types are XML content, albeit the XML content could be XHTML content. Reading the specification solves the problem with the phrase regarding the priority ordering of the MIME types, which generically says that a more specific MIME type is ordered before a less specific MIME type. This means application/xhtml-xml is ordered before application/xml and text/xml, because application/xhtml-xml is specifically formatted XML. The example HTTP conversation illustrated that the browser was explicit in what it wanted. There are browsers though that do not explicitly indicate what they want, as illustrated by the following HTTP conversation.
Request GET /services/historical/AMZN/2006 Accept: */* Accept-Language: en-ca Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50215; .NET CLR 1.1.4322)
Connection: Keep-Alive Some browsers send the Accept type identifier */*, which essentially means, “Send me whatever you’ve got; I will accept it.” Such a request is extremely unhelpful and makes it difficult to implement the separation of the resource from the representation. The solution to this problem is to define a default representation for the identifier */*. It’s not an ideal solution, but a solution begot out of the necessity to send something. Most likely, those clients that send */* are HTML-based Web browsers.
legal notice
Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.
Useful tools and features
related articles
Given the recent interest in Ajax, you’d be forgiven for thinking it was a new technology. In fact, the XMLHttpRequest object has been around for years. In technical terms, asynchronous JavaScript interaction with the server is nothing new. All of the other elements of the Ajax model have also been around for quite some time: CSS, (X)HTML, and DOM Scripting. Yet in 2005, interest in this methodology soared. Could it really be that simply giving this approach a snappy name like Ajax was responsible for the sudde...
2. Understanding the Definition and Philosophy of Ajax
The focus of this article is to provide solutions to some common, general problems and questions that are bound to arise before or during development of Asynchronous JavaScript and XML (Ajax) and Representational State Transfer (REST) applications. These common questions are not always technical in nature, often leaning more toward theory or philosophy of development. The problem with these kinds of questions is that once you begin to think about them, you keep going in a circle and end up where you star...
3. Understanding the Definition and Philosophy of Web Services and SOA
Understanding the Definition and Philosophy of Web Services and SOA Wikipedia offers the following definition of Web services:4 The W3C defines aWeb service as a software system designed to support interoperable machine-to-machine interaction over a network. This definition encompasses many different systems, but in common usage the term refers to those services that use SOAPformatted XML envelopes and have their interfaces described by WSDL. For ex...
4. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...
5. The Easiest Way to Get Started with Ajax and REST
The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you...
6. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...
7. Testing the Client Side Logic
Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two ...
8. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...
