Web Services
What is a Web Service?
Software system designed to support interoperable machine-to-machine interaction over a network. In simpler words we can call it as, Service delivered over the web.
3 Keywords:
- Web service is designed for application-application(machine-machine) interaction.
- Should be interoperable — not platform dependent(Platform Independent).
- Should allow communication over a network.
How does data exchange between applications take place?
Applications exchange data using request & response.
Let’s consider two Applications A & B
- If A needs some data or perform some action, A creates and sends a request to B.
- B gets the request and process it. And B creates a response and send it back to A.
How can we make web services platform independent?
Our web service application should be able to interact with any kind of application(whether it is a java application, .Net application, PHP application or anything).
The important thing for a web service to be a platform independent is that the request & response should also be platform independent. They should be in formats which are supported by all different kinds of platforms.
The two popular formats for request and response are,
1. XML — Extensible Markup Language.
2. JSON — JavaScript Object Notation.
How does the application know the format of Request and Response?
Every webservice offers a service definition, it is specified as follows,
a. Request/Response Format.
b. Request Structure.
c. Response Structure.
d. Endpoint.
Key Terminologies of Web Services
1. Request and Response
Request — It is an input to the webservice.
Response — It is an output from a webservice.
2. Message Exchange Format — It is the format of the request and response. Is it XML or JSON format.
3. Service Provider — It is the one which hosts the webservice.
4. Service Consumer — It is the one who consumes the webservice.
5. Service Definition — It is the contract between service provider and consumer. service definition defines,
a. Request/Response Format
b. Request Structure
c. Response Structure
d. Endpoint
6. Transport — It defines how a service is called. Is this service exposed over Internet or queue.
a. HTTP — It is a communication over the web. If we hit the url in the browser and the application will call the webservice.
b. MQ — It is a communication over a queue.
- Service requestor places a request message in the queue and the service provider will be listening on the queue.
- As service provider gets the requests and process it and it creates the response and puts it back to the queue.
- The service requestor would get the response from the queue.
This is called a webservice which is exposed over the transport MQ.
Web Service Groups
There are two web service groups,
- SOAP-based
- REST-styled
SOAP
It defines a specific way of building web services. In SOAP, we use XML as request exchange format.
SOAP structure has 3 things,
- Envelope — It has Header and Body
2. Header — Meta information, like authentication, authorization, signatures, etc.
3. Body — It is where we put content of request or response.
Message Exchange Format — SOAP uses only XML for Request and Response.
Transport — SOAP transports over MQ and HTTP.
Service Definition — SOAP uses WSDL(Web Service Definition Language).
WSDL defines,
a. Endpoints
b. All Operations
c. Request Structure
d. Response Structure
REST
It stands for REpresentational State Transfer. REST term is coined by Roy Fielding, who also developed HTTP protocol. The key thing about REST is, they make best use of HTTP(Hypertext Transfer Protocol).
HTTP also defines Request methods(GET, POST, PUT, DELETE) and it also indicates what action is been done. HTTP response includes the response status codes(200, 404).
The most important abstraction in REST is Resource. It is anything that we want to expose it to the outside world through your application.
A resource has an URI(Uniform Resource Identifier) and have different data representations,
- XML
- JSON
- HTML
Message Exchange Format — REST has no restriction. JSON is popular.
Transport — REST uses Only HTTP.
Service Definition — REST has no standard. WADL(Web Application Definition Language)/Swagger. Swagger is popular.
Next part coming soon. Happy Learning!