Web Services in detail

Web Services in detail

Learn about web service and how different services communicate with each other

A lot of you may have heard about web services but don’t know what they are and how they work. Well, just like any other concept, they are easy to understand if you interpret them right.

In this article, I will try to explain it in the best possible way right from the basics, differentiating it from many other similar concepts and why are they used extensively today.

Firstly, let's understand the difference between a website and a web application

A website is a collection of static web pages. Static here means that the data or information is hard-coded and do not require any inputs from a user or communications with the database. An example of a static website can be any small business website which has only services and contact us pages with content embedded in HTML itself.

Web applications, on the other hand, are dynamic websites which constantly require communicating with databases or back-end services for data or information to be presented to the end user. Examples of web applications are hashnode.com, amazon.com, etc.

A thought 💡

Do you think that massive applications such as Gmail, and Uber use a single programming language for all their development?

There are thousands of services running each second on their data centres to cope with users' demands and requests. These services may be executing various programs written in various programming languages and even running on different platforms.

Since there are more than one language and technologies used in developing one big web application, you might wonder how these different programs that are written in different languages communicate with each other.

If each language has its syntax and way of interpreting things, how do they understand what programs written in different applications wish to communicate?

Is there any standard way that all different programs can use to communicate with each other? (Just like each country have its native language, they can use English to communicate with anyone outside their country)

The answer is Yes. There is, and this is what we call a Web Service.

Web Service

Web service enables communication among different applications over the web using some standard protocol/method. Using web services, two different applications can talk to each other and exchange information.

Typically, a web service provider, who wants to provide its functionality to be used by other applications, develops a service and makes it available over the Internet. Any application can request the service from the server where it is hosted and the server responds to that request.

Two notable concepts come into the picture here — WSDL and UDDI. Putting in simple terms, WSDL(Web Services Description Language) provides information about the functionalities of any particular web service, and UDDI(Universal Description, Discovery, and Integration) is kind of a distributed directory of web services, where the server can publish their services, and clients can lookup to find any service. So, UDDI documents can have entries of multiple web services, and each web service may have its WSDL document.

The question arises again that how these applications communicate. The two important factors to think of are

  • The medium through which the data or information flows from one application to another

  • The format in which the information is transferred.

For example, while speaking over the telephone, the medium is the phone and the format is English or any language that both of you know.

Currently, two formats for communication between applications are universally accepted — XML and JSON.

Below is an example of the representation of students' data using JSON and XML.

Let's say there are 2 student records, each record has the student's name, email and city.

XML representation

<students>
    <student>
         <name>John</name>
         <email>john@xyz.com</email>
         <city>Boston</city>
     </student>
     <student>
         <name>Julie</name>
         <email>julie@xyz.com</email>
         <city>London</city>
     </student>
</students>

JSON representation

{
    "students": [
        {"name":"John", "email":"john@xyz.com", "city":"Boston"},
        {"name":"Julie", "email":"julie@xyz.com", "city":"London"}
    ]
}

You can see that JSON is easier to write and understand.

JSON is used on a larger scale than XML because it is lightweight and easy to understand.

Now, coming to the medium through which the data is transferred. Web services use HTTP(Hypertext Transfer Protocol) for sending requests and responses over the web.

HTTP uses a request/response technique, that is, communication or transfer of information between web server and browser is done through HTTP request and response packets.

For example, the web server is where all files related to the website are present; the browser or client sends a request for an HTML page, and the server responds with the requested page. Similarly, the browser may request some information from the server by sending an HTTP request, and the server processes the request and provides appropriate responses as an HTTP response in the format(XML or JSON) discussed above.

Now that you know the fundamental concepts of web service, let’s dig into the types of web services

Types of Web Service

Primarily, there are two types of web services-

  • SOAP web services

  • RESTful web services

In SOAP(Simple Object Access Protocol) based web service, all the information exchange happens over a single common format — XML. These XML-based messages have a defined structure, which is known as SOAP Message. Each of these SOAP messages consists of an Envelope, which has a header and body.

Remember that web services are used to communicate between two different applications, platforms, or browsers and servers, so these services need to have all information required for the efficient transmission of any message.

In SOAP envelope, the header provides information about the message itself and also includes routing information, and authentication information, if any. The body contains the actual data that needs to be sent.

RESTful web services communicate between applications using REST principles. REST(Representational State Transfer) is an architectural style which defines how communication happens between two different applications. And how it happens is uniform and stateless.

Two major principles of REST are uniform interface and statelessness.

A uniform interface means everything in RESTful web service is a resource. The resource can be data, information or any block of data. This means that there is no distinguished part which cannot be accessed or accessed differently, everything is accessed by a URI(Uniform Resource identifier).

Also, RESTful web services make use of HTTP methods for the exchange of information. Oh sorry, I meant the exchange of resources. 😄

For example, HTTP methods like GET, PUT, POST and DELETE can be used to request some resource specified in URI itself.

Suppose I want to get details of all student records from XYZ website, I can use GET

GET www.xyz.com/students

or if I need detail of a particular student with ID 12345

GET www.xyz.com/student/12345

Similarly -

  • PUT can be used to update the existing resource details

  • POST can be used to add new resources

  • DELETE can be used to delete a resource.

In the above example, I can use the PUT method and specify the updated value of any detail, like the name or email of the student in my body.

Similarly, I can use the POST method to add a new student record and the DELETE method to delete a student record.

If you are building a web service or an API in a RESTful manner, you must specify the appropriate functionality and response that you want to give for any given HTTP requests received through URI.

Stateless communication means there is no previous information or data that is required to send a current request or response. Every request or response contains all the information itself and does not rely on any previous state of messages.

REST also supports caching and layered approaches. Various layers of components can be added between the client and server like a proxy server, gateways, etc.

Also, REST supports XML and JSON format, whereas SOAP only supports XML.

Because of all these principles and differences, REST is preferred over SOAP.

Conclusion

Web services are widely used today for communication and exchanging data between disparate applications or platforms. Technology is growing at a faster rate and more connectivity and seamless integration of services ensure greater experience and value for users. RESTful web services are dominant for most reasons, and REST is widely used in developing APIs and web services today.


If you found this article helpful, please like, comment and share this article so that it reaches others. 😃

For more such content, please subscribe to my newsletter so that you get an email notification on my latest post. 💻

Let's connect 💫. You can follow me on