A RESTful API – also referred to as a RESTful web service or REST API. REST known as Representational State Transfer is an architectural style and approach to communications often used in web services development.
REST can be intellectually said as a Resource which could be a document or image, a temporal service or object, etc. There are some standards defined to write the code.
A RESTful API is an application program interface (API) that uses HTTP requests to perform below CRUD operations.
- GET – Used to retrieve a resource;
- PUT – Used to change the state of or update a resource, which can be an object, file or block;
- POST – Used to create that resource;
- DELETE – Used to remove it.
REST is useful in cloud applications because of the reason it is stateless. REST is also a language-independent architectural style. REST-based applications can be written using any language.
REST API Architectural Constraints
Client-server: Separation of concerns is the principle behind the client-server constraints. Separating the user interface concerns from the data storage concerns, the portability of the user interface across multiple platforms improves. The scalability becomes high by simplifying the server components.
Stateless: Each request from the client to the server should contain all of the necessary information for understanding the request. Taking advantage of any stored context on the server cannot happen. The session state is therefore kept entirely on the client.
Cacheable: Cache constraints require the data within a response to a request be implicitly or explicitly labeled as cacheable or non-cacheable. If a response is cacheable, then a client cache is given the right to reuse that response data for later equivalent requests.
Uniform interface: By applying the software engineering principle of generality to the component interface, the overall system architecture is simplified and the visibility of interactions is improved. Multiple architectural constraints are needed to guide the behavior of components to obtain a uniform interface. REST is defined by four interface constraints: identification of resources; manipulation of resources through representations; self-descriptive messages and, hypermedia as the engine of application state.
Layered system: The layered system allows an architecture to be composed of hierarchical layers by constraining component behavior such that each component cannot “see” beyond the immediate layer with which they are interacting.
Code on demand (optional): REST allows client functionality to be extended by downloading and executing code in the form of applets or scripts. This simplifies clients by reducing the number of features required to be pre-implemented.
The fundamental concept in any RESTful API is the resource. A resource is an object with a type such as a document or an image, associated data, collection of other resources.REST uses a resource identifier to identify the particular resource involved in an interaction between components. Resources that have data associated with them are part of the resource model for an API.
The state of the resource at any particular timestamp is known as resource representation. A representation consists of data, metadata describing the data, and hypermedia links that help clients in transition to the next desired state. The data format of a representation is known as a media type. The media type identifies a specification that defines how representation is to be processed. The specifications could be the address, link, id attributes, etc.
HTTP Response Status Codes
Below are some of the most common response codes that one might encounter while API Testing.
Benefits of Rest API
- It separates the user interface from the server and the data storage.
- It is independent of the type of platform or language.
- Reduces time and cost through sharing services.
- It helps in Increasing the collaboration throughout the R&D team members
Difference between REST and SOAP
|REST stands for Representational State Transfer||SOAP stands for Simple Object Access Protocol|
|REST is basically an architecture designed for web-based communication||SOAP is basically an SOA based architecture that can be used for middleware interoperability|
|REST uses URI to expose business logic||SOAP uses services interfaces to expose the business logic|
|REST permits different data formats such as plain text, HTML, XML, JSON, etc.||SOAP permits XML data format only|
|REST transfer is over HTTP only||SOAP uses HTTP, SMTP, FTP protocol to transfer|
|REST does not define too many standards like SOAP||SOAP defines standards to be strictly followed|
|REST calls services via URL path||SOAP invokes services by calling the RPC method|