REST API and open system messaging protocols

Lasal Hettiarachchi
6 min readApr 27, 2021

So I saw this meme the other day on reddit which got me thinking .Every service on the internet is implemented in the restful architectural style. So it is of essential for both front end and back end developers to properly understand the REST api and the open system messaging platforms such as Json and XML which are used to communicate among them.

What's a REST api?

Lets assume that you are trying to listen to that new drake album that your friend suggested . So you go to Spotify and search “Drake” and the list of all the albums pop up. Magic right? actually no . Its the power of the restful services.

What's actually happening here is when you type “Drake” in the search feild, an endpoint that is implemented in the backend to search artists is called and the results for that search query is returned to the front end by a JSON object. Lets try to brake it down

When we navigate to YouTube or Spotify the interactive user interface that we are presented and the underlying logic that runs in the browser is known as the front-end. The application logic, the computations to be done in the application and the responsibility of storing/retrieving data to and from the databases falls on the backend.(Although this line is getting narrow with JavaScript also supporting client side execution of logic). The frontend and the backend must seamlessly communicate with one another . But the caveat here is that unlike in the past , where everything from front to back was implemented using the same technology stack, the trending software development principle is to develop systems modularly paying respect for encapsulation and abstraction dialectics. So the front end might be implemented using popular JavaScript libraries such as react or angular while the back end might be implemented using asp.net , Spring or Node JS. The back end provides gateways through which the front end can call requests and receive the necessary payload of data. These gateways are formally known as endpoints. Representational State Transfer abruptly known as the REST api is the set rules that defines how the backend and the frontend should communicate with one another

Anatomy of a request

As mentioned earlier , REST is a set of guidelines that are set which defines how the api should be implemented. One such obligation is that the any frontend , despite the language that it was implemented it , should be able to call a specific URL and get the designated payload for that URL. This procedure is called a request and usually is executed via an http or https request in a distributed network. The payload that is sent back to the client is known as the response. (The client does not always have to be a pc, a mobile phone and other electronic devices are also capable of making http requests )

A request is comprised of 4 modules,

  1. The endpoint:
  2. The method:
  3. The header :
  4. The body :

https://www.facebook.com/lasalsandeepahettiarachchi/about

This is the URL of the about page of my Facebook profile. A URL format like this is very familiar to us. But lets actually try to understand what this really is by breaking it down. “https” is the protocol that is used to send the request. “www.facebook.com” is the destination of my request . “lasalsandeepahettiarachchi” and “about” is the route within that destination. So what's actually happening here is I’m sending a get request to the Facebooks server asking to return the about page of the relevant profile by giving a unique identifier.

sounds gibberish? lets further investigate.

http://localhost:8080/api/item

This is a URL of a shopping cart application that I’m currently developing. As above the “http” is the protocol that is used to send the request. But contrary to the above instance where the application is hosted in the Facebooks servers, my application is hosted in my machine (during the development phase) hence localhost. :8080 is the port number in which the server is running on. “api/item” is the route within my application which returns the list of all the items in the system. this URI that is invoked in a request is known as the endpoint and it is always unique in a global context.

The request also contains a method defining what is the action that has to be performed by the request. There are many methods . But let us look at some of the important one’s.

  1. GET : The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
  2. POST : A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.
  3. PUT : Replaces all the current representations of the target resource with the uploaded content.
  4. DELETE : Removes all the current representations of the target resource given by URI.

The request also contains a header which includes resources to authenticate the user such as JWT tokens.

The body contains the parameters needed to perform the above tasks. This may include json objects.

When a request to the backend is sent , it will most likely than not , will return a payload depending on the request. This payload contain the feature of language inoperability and should also be human readable. There are several open system messaging protocols that are used to send data. In this article , we will explore 2.

  1. XML : extensible Markup Language
  2. JSON : JavaScript object notation

XML .

XML structures its data by marking it with descriptive tags .

Here the Employee object has the attributes name ,salary, designation and address. address is a nested object which contains attributes such as , city, lane, state and zip code.

XML is an open standard defined by W3c. Therefore it can easily be used to send data across heterogenous environments.

JSON

JSON is also another such standard which is becoming extremely popular due to its resemblance with java script. Here the object is returned as a JavaScript object or an array of JavaScript objects.

Here we can see 2 JS objects which are items in the shop. first is Grapes and the second is pineapple. Both of them are returned inside an array , hence they are enclosed within “[]”. “{}” is the JavaScript object notation. everything that is described within these are attributes of the object. These are given as key value pairs separated by commas. The first is the key and which always takes the form of a string and the second is the value which can take many data types such as String , Int , Double, Date-Time or even another JS object.

This is a general introduction to the REST api and the open system messaging protocols that are used to communicate within a distributed system. I hope you found it useful

--

--