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
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,
- The endpoint:
- The method:
- The header :
- The body :
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.
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.
- 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.
- POST : A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.
- PUT : Replaces all the current representations of the target resource with the uploaded content.
- 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.
- XML : extensible Markup Language
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.
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