A Comprehensive Introduction To REST API
All web based applications, whether a CMS or an online store, carry out three fundamental actions.
- POST/GET (creates and/or updates information)
- READ (query information)
- DELETE (removes information)
Together, these three actions form the core of Create, Read, Update and Delete (CRUD) operations. Whenever a user interacts with a web based application, the underlying mechanism utilizes a combination of CRUD to return the desired information and carry out the specified actions. In order to make sure that CRUD actions are facilitated at all times, Representational State Transfer (REST), a robust application architecture is highly recommended.
What is REST
REST is an application architecture model that is usually implemented in the form of functions that send requests and receive responses via Hypertext Transfer Protocol (HTTP). The best thing about REST is that it is not strictly implemented in any particular language or format. Rather, the protocol could be easily integrated into any web app using any language, as long as the core constraints of the REST are in place.
This ease of implementation means that in real-world apps, servers and clients are independent of each other. The result is truly distributed web applications that are able to use and service a huge range of services and platforms without any worries of programming language or communication barriers. Using REST APIs, all web apps that use HTTP (that includes everything on WWW!) is able to link and exchange data in a very simple and easy to implement format.
Resources
Resources are the core building blocks of a REST API. These are discrete and independent entities with a unique identification. Each resource is accessible through a unique address called Uniform Resource Identifier (URI). REST APIs use URI to access and interact with the resources.
A resource could have several representations that are different implementation of the same resource. For example, image is one type of resource. However, images are available in various formats such as GIF and JPEG. All these formats are the representations of the same resource (image). The relationship between a resource and URI is one-to-many. A single URI represents a specific resource but a single resource could have multiple URIs because of multiple representations.
Since there is no set implementation of REST, the developers have a lot of leeway in the matter. However, in order to qualify as REST, an API should have three important characteristics.
Client-Server Architecture
In a REST API, client(s) and server(s) are independent of each other. Both components are independently implemented and could be easily be replaced or re-implemented in another language without breaking the app.
Stateless
Being stateless means that servers do not store any data of the interactions with the clients. This greatly simplifies app design and implementation as clients provide all required information for every interaction. In addition, the load on the servers decreases proportionally. In REST APIs, statelessness is at the core of the design because HTTP itself is a stateless protocol.
Cacheable
In REST APIs, clients could store or cache responses returned by the servers. For the end-users, this greatly speeds up the response time of the web app. All REST APIs are cacheable because the underlying protocol HTTP offers this capability.
Benefits of REST
REST offers several important benefits to developers and users of web applications.
Freedom of Implementation
Freedom of implementation is perhaps the greatest benefit of REST APIs. Both servers and clients could be implemented in any language or platform. This is made possible because all responses are required to be in popular information exchange formats such as JavaScript Object Notation (JSON) or XML. Thus, clients and servers could be implemented in different languages without any fear of information loss.
Separation between clients and servers
This central idea of separation between clients and servers ensures that all REST APIs are very portable and diverse. The developers on either end could freely change and replace components without any problems for the web app.
Scalable and Reliable
The above two benefits combine together to make sure that all REST APIs and the web apps implemented using these APIs are scalable and reliable in the extreme. Since clients and servers are independent of each other, either end of the web app could be scaled up without breaking the web app. Both ends of the web app (the server and the client) could be coded and implemented in any language as per the requirements of the business model and demands of reliability.
WordPress REST API
WordPress REST API is perhaps the most popular and widely used REST API in the world. It is available to everyone who uses WordPress for the development of websites, online stores and web apps.
REST API in WordPress is available as a plugin that is still in beta stage. The plugin has become the focus of the community because of the importance of REST structure.
Resources
WordPress REST API works with a wide range of resources that encompasses the entire range of resources available in WordPress. In particular, WordPress REST API uses posts, pages, media, custom post types, post meta, revisions and comments. All these resources are identified and accessed using URI.
Implantation of WordPress REST API
WordPress REST API relies on HTTP to implement the core requirements of the REST. HTTP requests are used to perform Create, Read, Update and Delete (CRUD).
The commonly used HTTP request variables are GET, POST, PUT, DELETE, HEAD and OPTIONS. Clients use these request variables to communicate and send information to the servers.
In response, servers use a range of codes to signal various results and events. Some very common codes are 200 (OK), 201 (Created), 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), 405 (Method not Allowed), 410 (Gone), 500 (Internal Server Error) and 501 (Not Implemented).
JSON for Information Exchange
In order to make sure that servers and clients remain independent of each other, WordPress REST API uses JSON, a lightweight information exchange service. Given the wide range of platforms that work with WordPress, JSON is the perfect technology to act as intermediary between servers and clients that employ WordPress REST API.
JSON is especially used to build applications with lightweight frontend and WordPress powered backend. These apps are designed for a wide range of devices that could leverage the ease of running the lightweight frontend and the power of WordPress excellent processing prowess.
Conclusion
REST APIs, particularly WordPress REST API have become the de facto standard of building web apps. REST APIs exposes clean interfaces that provide access to various resources of the platform.
Comments
Post a Comment