Representational Design
Last updated
Last updated
The output of your API is extremely important. The goal of representational design is to create some level of consistency in order to facilitate the client's consumption of your service. This section goes over thoughts and considerations that should be made when designing the output response of your RESTful API.
JSON has definitely taken over XML in popularity when it comes to building internal RESTful APIs. Most newer systems by companies will most likely only support JSON for the client to consume. It isn't uncommon now for newer developers to have never even worked with XML in their life. Larger companies who provide a public RESTful API will generally support both JSON and XML, Twitter is a great example of this, but that is quickly changing day by day.
At the end of the day, supporting JSON and/or XML is a decision that only you can make. My recommendation is that if you are building an internal RESTful API, pick one format and stick with it, it will most likely be JSON. If you are creating a public RESTful API, prioritize JSON over XML.
Consistency is key when building a RESTful API. A consistent output response makes the lives of both the consumer and other developers building the API much easier, because a consistently wrapped response is self-documenting for both the client and server. There are really only 2 types of responses, a success response and an error response. Below are examples of each.
I personally use this format for all of my RESTful APIs I build.
Code – This can be the HTTP status code or some sort of internal status code.
Status – This can be either "success" or "error" depending on the state of the call.
Message – This is for both the client consuming the API and the developers creating the API so that it may be use for documenting and debugging purposes.
Data – That contains the meat and potatoes of the API, usually it is the specified resource or a collection of resources that is being asked from the client.
A successful response in a consistently wrapped style may look something like this:
Here is an example of an error response in a consistently wrapped style output:
Whatever style of output response you choose to have, the key thing here is to always keep it consistent.
Security is a big aspect when creating RESTful APIs. A common mistake I've seen from developers is when an output response mirrors the internals of a database schema. This is extremely common due to the increasingly popular powerful ORMs (Object Relational Mapper) that developers have access to today. Instead of exposing every single possible field from the database, it is important to mask the data via transformers and DTOs.
Do note that we will talk more about DTOs, also known as Data Transfer Objects, in the Input Request section of chapter 5.
Example, consider the following User model created in mongoDB.
If we were to simply use the ORM as follows.
We would expose the password field in out output.
When following HATEOAS and creating links in our RESTful API, a consistent and intuitive representational form should be used to depict the resource's links and relations.
Take for example the following resource:
A "_links" key-value pair is provided. Each key in "_links" provides some sort operation on the resource. In this case, "self", "parent", and "fire_player" are operations of the player. The "self" represents the resource itself whereas "rel" and "href" represent the relationship of the link relative to the current resource and the link it goes to. Other optional fields like the HTTP verb "method" and "description" can also be included to provided further self-documentation. Later in this book, we will go deeper in the topic of HATEOAS and linking.
Whatever format or types of representation you choose for links in your RESTful API, as long as it is easy to understand and self-explanatory for the client, then everything should be fine.