API/Web Service Overview:
So lets start off by learning what exactly is a Web Service?
Its a method of communication between two applications or electronic devices over the worldwide web.
Here is an example:
Consider a flight website, In the British Airways website they want to add an additional feature that is providing hotel options in their website. They would want to partner with a hotel company for integration of code.
Now this wouldn’t be so easy as both would mostly be in two different platforms(Example: consider British Airways site developed in Java and hotels.com website developed in Dot Net)
The task would get complex and all that British Airways website requires is one feature that is to be able to book a hotel in their website.So ‘hotels.com’ says they can provide a web service/API. This is nothing but a block of code wrapped as a webservice and it will be exposed to British Airways website as an API/Web service.
plays an intermediate role between British Airways and Hotels.com
is independent of any language and tool
It works on http protocol
In our example, hotels.com exposes its API to other flight websites so that these sites can get the list of hotels and display them to the users.
What does an API/Webservice do?
When you give the destination details and hit the service[Destination search web service]
The service will talk to hotels.com code , gets the details of all hotels in the destination and gives response back to British Airways website
Communication between web services can be done using SOAP and REST
The language used for communication can be in XML(SOAP) and Json (REST)
SOAP(Simple Object Access Protocol)
Is a protocol used to send and receive messages between applications without interoperability issues(Eg: different platform, different OS)
Consist of predefined set of tags
Follows a standard in sending requests to a web service
A Soap message consists of a SOAP envelope,SOAP header, SOAP body,SOAP fault
REST ( Representational State Transfer)
Communication between Client/Server,
Called as Restful Web Services meaning a service which uses Rest API to communicate.
Rest is not a protocol, its an architecture
They are simple and standardized
Scalable/Stateless- When the service gets complex easy to makes changes, need not keep track of what data is in which state
High performance, supports Caching
The main building blocks of the REST API are
Request-sent from client to server
Response-received back from the server( Json response)
There are operations called CRUD
Following are http methods equivalent to CRUD
Create-Uses POST method
Update-PUT /PATCH method
Lets discuss this with an example
We have an ice cream store and have built a web application to show the flavors of ice cream and their availability
Endpoint would be eg: https://www.icereamshop.com/API/Flavour
Flavor- known as a resource and signifies we are working with Flavors resource in REST API
The pieces of the request are endpoint, operations/method, parameters/ body.
The response will be in the form of Json format
1.To display/get current ice cream flavors in stock
We use a GET method, Endpoint is /API/Flavor
Response is the available flavors in stock
2.To update a flavor(eg:chocolate cookie dough)
We use PUT method, Endpoint is /API/Flavor/1(/1 is to indicate the idea of 1)
In the body of the request, give the update flavor :chocolate cookie dough in our case
The response would display the updated flavor
3.To introduce a new flavor(Eg: sitaphal)
We use POST method, Endpoint is /API/Flavor
In the body of the request, we enter the new flavor and send the request
The response shows the new flavor created(this data gets posted in the server)
4.To remove a flavour
We use DELETE method
The response shows the flavor of the ice cream we wanted to remove(this data gets deleted from the server)