In this post I relate to you how components are represented in REST APIs and outline how to do the in a standard and meaningful way. There four key aspects to consider while thinking about representational design. In each aspect, I have tried to explain the rules that govern what kind of forms and formats are to be used.
1. Message Body Format
REST APIs often employ a text-based format to represent a resource state as a set of meaningful fields. Today, the most commonly used text formats are XML and JSON.
Following are some rules regarding the format of the Message Body
- Rule: JSON should be supported for resource representation
As a format for data exchange, JSON supports lightweight and simple interoperation:it does its job. Today, JSON is a popular format that is commonly used in REST API design. JSON borrows some of JavaScript’s good parts and benefits from seamless integration with the browser’s native runtime environment.
- Rule: JSON must be well-formed
A JSON object is an unordered set of name-value pairs. The JSON object syntax defines names as strings, which are always surrounded by double quotes. The following example shows this;
{ "firstName" : "Osvaldo", "lastName" : "Alonso", "firstNamePronunciation" : "ahs-VAHL-doe", "number" : 6, "birthDate" : "1985-11-11" }
- Rule: XML and other formats may optionally be used for resource representation
REST APIs may optionally support XML, HTML, and other languages as alternative formats for resource representation.
- Rule: Additional envelopes must not be created
A REST API must leverage the message “envelope” provided by HTTP. In other words, the body should contain a representation of the resource state, without any additional, transport-oriented wrappers.
2. Hypermedia Representation
Hypermedia, an extension of the term hypertext, is a nonlinear medium of information which includes graphics, audio, video, plain text and hyperlinks. Hypermedia may be developed a number of ways. Any programming tool can be used to write programs that link data from internal variables and nodes for external data files.
In his thesis, Roy Fielding describes four interface constraints for REST. These are
- identification of resources through URIs
- manipulation of resources through their representations
- self-descriptive representations
- hypermedia as the engine of application state.
The rationale behind this fourth constraint is that, by embedding actions controls and URIs within each representation, the client remains independent of the server, and therefore can be universal. The representation that the client receives would include possible new state changes, the URIs and methods that could be used for those transitions, and any associated ordering semantics.
The idea of hypermedia embedding all the action controls necessary to interact with the server works well for an arbitrary number of universal clients interacting with a given server. In this case, the server offering a set of resources specifies all the ordering/interaction rules within the representation.
Most application clients, on the other hand, interact with more than one server, and any given server cannot set the ordering constraints. The clients know how to compose applications out of resources offered by various servers, and each client needs to be able to exercise control over composition. To be able to exercise such a control, client applications cannot be universal, and the benefits that John lists above cannot be completely realized.
- Rule: A consistent form should be used to represent links
- Rule: A consistent form should be used to represent link relations
- Rule: A consistent form should be used to advertise links
- Rule: A consistent form should be used to represent media type formats
3. Media Type Representation
Programmers working with the Web are familiar with modeling informational in multiple domains and formats a structured representational form, which is carried by an individual HTTP request or response message body, is analogous to an instance of a schema class
- Rule: A consistent form should be used to represent media type schemas
- Document
- Class
- Storage
- Controller
- Rule: A consistent form should be used to represent media type schemas
4. Error Representation
HTTP’s 4xx and 5xx error status codes should be augmented with client-readable information in the response message’s entity body. The following rules present consistent forms pertaining to errors and error responses.
- Rule: A consistent form should be used to represent errors
- Rule: A consistent form should be used to represent error responses