Toolsnip

What is REST, and how does it work?

Fullstack Developer Interview Questions and Answers

Short Answer

REST stands for Representational State Transfer. It is an architectural style that defines a set of constraints and properties based on HTTP.

Detailed Answer

REST (Representational State Transfer) is an architectural style for designing networked applications. It relies on a stateless, client-server, cacheable communications protocol — the HTTP.

In RESTful systems, resources (data or services) are identified by URIs (Uniform Resource Identifiers). These resources are represented in a format that clients can understand, such as JSON, XML, HTML, or plain text.

REST uses standard HTTP methods such as GET, POST, PUT, DELETE, etc. to perform operations on these resources. Each method corresponds to a specific operation: GET retrieves data, POST submits data, PUT updates data, and DELETE removes data.

The statelessness constraint means that each HTTP request from the client to the server must contain all the information the server needs to fulfill the request. The server should not store any information about the client's state between requests.

Cacheability is another important constraint. Responses must implicitly or explicitly define themselves as cacheable or non-cacheable. If a response is cacheable, the client can reuse that response data for later, equivalent requests.

RESTful services often use JSON as the format for data exchange, which is lightweight and easy to read and write. This helps in improving the speed of the application by reducing the payload size.

One of the key principles of REST is the uniform interface. This simplifies and decouples the architecture, which enables each part to evolve independently. The uniform interface constraint is broken down into four guiding principles: resource identification, resource manipulation, self-descriptive messages, and hypermedia as the engine of application state.

Resource identification in requests is achieved through URIs, which provide a way to locate resources in a RESTful service.

Resource manipulation is performed using the representations received from the client. This allows the service to remain stateless, as each request contains all the information needed to process the request.

Self-descriptive messages include all the information needed to understand and process the request. This includes metadata about the data and the actions that can be performed on it.