In the current programming landscape, the REST design pattern is widely used for its flexible capabilities. REST developers can implement it in a variety of ways. In the event that a client request is made over a RESTful API, it replies with a representation of the state of the resource to the requester, in the form of a language-agnostic type of file, e.g. a JSON representation or an XML representational data format.
For example a usual mobile application will pull the data it needs in form of a JSON or XML representation. Consider a news app in iOS and android, the app from itself doesnt has any of the headlines and stories but it pulls that data from the server of the news app company. The data format in which the server provides a response is in either JSON or XML format but mostly in JSON.
This method is the most popular because, despite its name, it is a reading and writing format for humans and machines alike.
Example defining a REST API –
In our use case where we will be developing a large scale realtime web application, lets consider that we will be developing a realtime chat application for iOS and android users. For that we will need to have a backend server which will be able to hold the chat messages which a user sends to another user. The chat message needs to be stored in the server until the recipient user receives that message and then the message will be removed from the server. For this purpose we need to have a specific web URL to which an http post request has to be made in order to send the chat message from user’s mobile application to the server. After that whenever the another user comes online their mobile application will send a broadcast notification to the chat application web server that the particular user has come online and if there is any pending message to be received by the user the server may send the message. The handling of all of this whole process will be fulfilled by a RESTful API.
Lets consider another example, suppose we have just logged in to our Facebook application from our mobile. Now all the latest feeds and updates will be requested to the Facebook backend server by the Facebook mobile application via the company’s RESTful API. The Facebook mobile app will request a specific URL defined by the company and request all the latest data and updates by making a GET request to their backend REST API.
Another important thing to keep in mind is that HTTP headers and response headers define the authentication, authorization, date modifications, location, caching, cookie modifications, URI, and many more properties of the request and response respectively. It is important that requests and responses also contain information which identify them uniquely, so to use appropriate information to identify them in a web server request.
In this image we can see that a sample request to get the latest news was queried to the bing news search API and in response the bing API sent a response back in JSON format.
Uses and benefits of REST API
– A REST API allows users to access resources without knowing the implementation details of the service. This is possible because the API uses a uniform interface that is easy to build and maintain. This makes the API usable by a variety of clients and it can be changed without much effort.
– A REST API is a self-descriptive interface. This means that clients can easily determine if a service supports certain operations without having to parse the API documentation. The interface for a REST API can be written in a human language, which allows clients to understand it.
– A REST API is stateless. This means that resources can be accessed without the need to maintain a connection between the client and the server. This eliminates the need to keep track of the state of the client.
– In a REST API, the client interacts with the service via HTTP, which is a stateless protocol.
RESTful APIs are useful for web use and are ideal for cloud applications because they do not rely on stateful components to be shared. Stateless components can be freely redeployed and generally scale to handle changes to loads.
This lets them better scale with use than stateful components. In designing REST APIs, you also control how a URL is decoded. Microservices and cloud computing are improving the advantage of RESTful API design.
How RESTful APIs work
– REST enables a client computer to request data and information from a web server.
– The term REST is used by many different people to describe different concepts. Also included in RESTful API management platforms are tools for creating and working with APIs. RESTful APIs are also known as RESTful Web Services because they are defined using HTTP.
- Requesting a RESTful API requires a URI (Uniform Resource Identifier). A URI is a string of characters that identifies a network resource. RESTful APIs use HTTP methods such as GET, PUT and POST to request and send data to a service. The RESTful API management platform provides a URI for each API.
RESTful allows a transaction to be broken into modular units. This modularity is achieved by using commands instead of the data itself to request an underlying piece of functionality. By convention, the transaction must eventually return a resource representation suited to display in a browser or support business-specific needs such as token generation written in a programming language. Common topics addressed by RESTful API’s are:
– GET retrieves a resource; – PUT changes the state of a resource; – POST creates a resource; and – DELETE removes a resource.
In the Bing search query we made a GET request to its API.
- POST all of the information, e.g. user agent, cache control
REST design principles
REST APIs are based on the idea that clients and servers should communicate using the same protocol in order to clearly define the interface between applications. REST APIs are designed to be easily consumed and implemented in a variety of languages and environments. The API is usually defined in a format that is independent of the application that uses it. This can be achieved by using a hypermedia format such as JSON, XML, or a binary format such as Protocol Buffers. Routing is achieved through the use of URIs (Uniform Resource Identifiers), which are strings that represent the unique location of a resource on the web.
We can look at a URI made through routing in our example of the Bing search request.
Here we can see that the request URI contains the version number which is 7, we are here accessing the api.bing subdomain of the Microsoft website in order to access the search query we need to perform.
The REST architectural style
The REST architectural style is an architectural style, based on the idea of a request-response paradigm, that enables clients to interact with resources on the web, and that defines a set of constraints for implementing and designing web-based applications. The REST architectural style is widely used by web applications such as Google, Facebook, Twitter, and Amazon.
An API is a communication platform that connects web applications or services within different domains. An Application Programming Interface (API) is a standardized way for client software to access servers that host resources, such as data or application logic. REST APIs can be developed in any programming language and support a variety of data formats. All that’s required is that the APIs align to the Requirements for Transfer of Non-Referential Information (REST).
All REST API requests should look the same because they follow the same protocols, it would be confusing and difficult to use two REST API’s. API’s should contain every piece of information that a client requires to access. Resources shouldn’t be too large, but should contain every piece of information that the client might need.
Freedom from Client-Server Coupling. Clients and servers must be completely independent, and that means no interaction between them other than transferring data. Servers shouldn’t be allowed to modify the behavior of clients, and clients shouldn’t ask servers to modify the client.
REST APIs are stateless
REST APIs are stateless meaning that each request requires all information related to the request including the client’s IP address, and the date and time the request was submitted. Most server applications aren’t allowed to store any data after processing a request. It’s important to make requests cacheable, so as to prevent servers from becoming overwhelmed during peak load times. CACHE headers help identify if caching is allowed. The goal is to make requests faster on the client side, but increase scalability on the server side.
Stateless interface: The web service should be processed by a client without storing or maintaining any knowledge of the client’s identity or state.
Layer system architecture
In REST APIs, the calls and responses are communicated through different layers. There may be a number of different routing nodes among the communications. Browser applications typically communicate with an application server via a REST API and might share resources with client applications or other external endpoints at the operating system or device level. However, there are many cases where the browser will communicate directly with the client application using URL requests that only include static resource identifiers. These static resource identifiers cannot route the information to an internal processing node such as a server. Core applications such as Web browsers notify application service providers (APPs) when new data is added to the browser.
RESTful API Design and Architecture Constraints
The more common notions for designing an API were defined by Roy Fielding in his 2000 doctorate dissertation. In contrast to the previous literature, the formal definition offers clear constraints for RESTful API design: Requests should have a URI that describes a resource. The method of a request should be described by URL parameters. The resulting representation of a resource should be unchangeable by a DELETE request.