Understanding of Web Service & REST API

What is API?

API (Application Programming Interface) is a particular set of rules (‘code’) and specifications that software programs can follow to communicate with each other. For example, you have a hardware button on your TV which turns it ON/OFF. Now suppose you want to turn ON the TV from a distance. You just press the ON/OFF button on a remote control and the TV set action on the command given by the remote control. This is how API works! Think of TV as a software program having services and resources, and the TV Remote control as another software program which use these resources. The method (or radio/infra red waves) through which a remote control communicates with a TV set is an API.

When and where it is used?

It’s used when 2 or more separate application need to work together to achieve something they can’t do alone.When interaction involve sending data over network, at that moment Web Service comes into the picture.

What is Web Service?

As the word sounds, they are not new fundamental business concept, it just a technology which permits old business concept to work together over the internet using some protocol. i.e. Oxford Dictionary API, in which you will send word, and in return it gives meaning of that word.

In simple way Web Service is nothing but a message passing (In XML & JSON format) between two machines. A client sends request over the Internet and a server receives that request, processes it and returns a response. When a browser makes a request for a webpage, it receives HTML and other related content in the response. But when it just asks for data and uses JavaScript or other client-side code to process the response, a web service has been used.

Modern web service communications are nearly always handled over HTTP. But the format of the message that are being sent and received can be different. Let’s consider one scenario for more understanding,

One service provider (A) is building application which shows latest designer clothes like amazon. For that he need to set up database to add products information (maybe it’s price, product name, images etc.) and one program to get that resources and display on application(Android, iOS, Web client) which is called as API call.

11
            Fig 1. Understanding of web service

 

Note: Mark that this is limited to his application only, this will not allow other application to access his local data.

Now there is one another provider (B) also want to build same kind of application, So, B has two option:

  • Either he do same stuff as A does. (make API)
  • Or tell A to give his API & allow access to his database to retrieve & modify data. (A’s API called over web –Web Service)

So now what will happen B will request for A’s API by passing request parameter (required for API) and at the back-end API call translate to SQL queries and gives SQL result into result suitable to API response.

An API is almost always better. You’ll get significant benefits in terms of re usability of the data and portability of your code, as well as avoiding coupling (interaction of application with application’s database) between your app and the database. But this scenario is works without network.

Because as you know Web service facilitates interaction between two machines over a network. But API acts as an interface between two different applications so that they can communicate with each other.

Summary:

  • All Web services are APIs but all APIs are not Web services.
  • API does not need to be web based.
  • API Will not explore the database but Web service do
  • A Web service uses only three styles of use: SOAP, REST and XML-RPC for communication whereas API may use any style for communication.
  • Web services might not perform all the operations that an API would

So, it’s raised some of the question like, what’s the message format?  Some web services use SOAP, a particular XML format, others use generic XML mark up and still others use JSON. (JavaScript Object Notation). And how do you formally request? Said differently, what is the required syntax of the service requests? Do you make requests with named methods using syntax that’s similar to client-side programming? Or do you use Uniform Resource Identifiers? Or URIs that are sent as HTTP requests. What are the names, data types and potential values of parameters that you send in as part of the request?

What is REST API?

One Architecture which is used to build client/server network application. REST stands for REpresentational State Transfer. REST is not a standard it’s a style (architecture) of describing the transferring state of something by its representation. Implementing REST is very simple as compared to other methods like SOAP, CORBA, WSDL etc., it basically works on HTTP protocol.

REST describes a set of architectural principles by which data can be transmitted over a standardized protocol (such as HTTP). REST does not contain an additional messaging layer and focuses on design rules, its use http protocol, and http header include every information which you need, you just have to pass body information with representation.

The key attributes of a RESTful applications are: All communication is via http GET, POST, PUT, DELETE AND all items are addressed via a standard URL of the form http://www.optimumbrew.com/app/appname/endpoint

i.e. only a pure URL with no parameters ,URL identifies the thing.  And the GET, POST, PUT, DELETE identifies what you want to do.

There are brief benefits of RESTful application:

  • The RESTful Web services are completely stateless (no record of previous interaction).
  • There is no standard set of rules to describe the REST Web services interface. (The service producer and service consumer need to have a common understanding of the context as well as the content being passed.)
  • REST-full api is platform-independent.
  • REST service is lightweight because you do not need to release changes to clients. You simply make changes to your logic, retaining URLS and the response should remain the same.
  • REST services are easy to integrate with the existing websites and are exposed with XML so the HTML pages can consume the same with ease.There is hardly any need to refactor the existing website architecture. This makes developers more productive and comfortable as they will not have to rewrite everything from scratch and just need to add on the existing functionality.
  • Reduce the complexity of our communication by its representation (We do not have to discuss about our needs.)
  • A client can access the resource using the unique URI and a representation of the resource is returned.
  • REST uses a smaller message format than SOAP.
  • REST provides better performance, as well as lowers costs over time. Moreover, there is no intensive processing required, thus it’s much faster than traditional SOAP.

At the end, when you’re publishing a complex application program interface (API) to the outside world, SOAP will be more useful. But when something with a lower learning curve, and with lightweight and faster results and simple transactions (i.e., CRUD operations) is needed, REST is preferred.

How you will use REST API?

Let’s continue with our previous example of two service provider A & B. Now we consider A as an API provider & B as an API consumer.

To pass data over the internet you definitely need to deal with JSON formatted data in your application.

JSON: Java Script Object Notation is one format that is used for data interchanging in web service response instead of XML. Because it is light weight as compared to XML and less network bandwidth consuming.

Fig 2. Client Server Communication
Fig 2. Client Server Communication

References

  1. http://stackoverflow.com/questions/7440379/what-exactly-is-the-meaning-of-an-api
  2. https://technicalwritingtoolbox.com/2012/03/27/what_is_api_documentation/
  3. http://www.differencebetween.net/technology/internet/difference-between-api-and-web-service/
  4. http://programmers.stackexchange.com/questions/145605/rest-vs-restful-vs-normal-web-service-the-same-or-not
  5. http://searchsoa.techtarget.com/tip/REST-vs-SOAP-How-to-choose-the-best-Web-service

Leave a Comment