RESTful services

Hasaranga Wijesinghe
3 min readMay 15, 2022

what is REST?

REST is an acronym for “REpresentational State Transfer protocol”. It’s a type of distributed hypermedia system architecture. Because they’re simple to create and consume, REST has quickly become the de-facto standard for establishing web services on the internet.

REST contains guiding principles and limitations, much as other architectural methods. If a service interface is to be called RESTful, these principles must be followed.

In REST services

  • Resources are identified by a URI.
  • Multiple URIs may refer to the same resource.
  • Client and Server communication
  • JSON or XML is used to pass data
  • Lightweight, Scalable and maintainable

RESTful key Elements

REST is a method of gaining access to resources in a certain context. For instance, you may have a server that stores crucial papers, photos, or movies. These are only a few examples of resources. If a client, such as a web browser, requires any of these resources, it must submit a request to the server. REST services now define a method for accessing these resources.

The Key Elements of RESTful implementation are as follows

  1. Resource — The first key element is the resource itself
  2. Request Verbs — These describe what you want to do with the resource. A browser issues a GET verb to instruct the endpoint it wants to get data. However, there are many other verbs available including things like POST, PUT, and DELETE.
  3. Request Headers — These are additional instructions sent with the request
  4. Request Body — Data is sent with the request
  5. Response Status codes — These codes are the general codes that are returned along with the response from the webserver.
  6. Response Body — This is the main body of the response

RESTful Methods

  1. POST — Insert a new resource
  2. GET — Fetch a resource
  3. PUT — Replace an existing resource
  4. DELETE — Remove a resource

RESTful has 6 Constraints

  1. Uniform Interface
  • Based on the HTTP specification
  • URIs refer to resources and HTTP verbs are actions performed on resources.
  • HTTP verbs (GET, PUT, POST, DELETE).
  • URIs (resource).
  • HTTP request (header, body, query parameters, and URI) and response (header/status and body).
  • Use hypermedia to better navigate through resources

Stateless

  • No client state on the server
  • Self-containing messages. Each message contains sufficient information for that particular operation
  • Session state will be kept on client-side

2. Client-Server

  • Use URI to make the connection between the two.
  • Clearly separates user interface and services. The client is portable, the client does not have any connection to the data server. The server stands independent of different user interfaces.
  • HTTP stack is the communication platform.

3. Cacheable

  • REST services should be cashable.
  • Resources returned from the server should be cashable.
  • Client can cache resources (implicit caching).
  • Server determines what and how should be cached (explicit caching).
  • Server-Client negotiates the caching scheme.

4. Layered System

  • Client does not see the underlying layers or complexities of the service.
  • Client only knows the URI.
  • Server is decoupled enough to have multiple layers and intermediate services sitting in between client and server.
  • Client only deals with the abstraction of resource URI and verb.
  • Highly scalable

Best Practices

Use Nounover Verbs. Do not use the GET method to alter the state, use plural nouns, always represent subresources, User HTTP status codes for errors, Use Links to other related resources where needed, and Provide additional functionalities like filtering, pagination, sorting, and field selection via query parameters and Always expose via HTTPS

--

--