1. Introduction to API/Webservices/Rest API
Welcome you all to the brand new ‘API Testing’ blog series! In this entire series, we would be learning about REST API Automation Testing with Java, Postman. We would be starting with basics and then move to advance topics. So let’s begin!!!
2. What you will Learn in this blog?
- Quickly understand ‘Communication’
- What is API
- Practical usage of API
- A very simple (raw) json
- Client-Server Architecture
- HTTP Protocol
- Difference between Webservice and API
3. Quickly understand ‘Communication’
Before we understand API, let us quickly understand about ‘Communication’. We know that some medium is needed for communication to happen.
So, let us say we open a chrome web browser and type https://rahulshettyacademy.com/ on the browser address bar and Hit Enter. The request would go to an API and the API forwards the request to respective domain ‘rahulshettyacademy’. The domain (if found), responds back to API and API responds back to web browser. So API acts as an interface between web browser software and website (domain) software. The below diagram explains the same
5. Practical usage of API
Launch https://www.cleartrip.com and search for a Delhi-Bangalore flight. We get a lot of search results from different airline companies (Air India, Indigo etc)
Now, “cleartrip” is not an airline company, it is just a 3rd party software vendor (just like makemytrip, goibibo, yatra etc). Now, how come “cleartrip” company is having the information from different airline companies like Indigo, AirIndia etc? The answer to this question is that, “cleartrip” does NOT have access to the database of airlines (Indigo etc). Instead, airlines expose some of their APIs to 3rd party websites so that a person can buy tickets on these websites. So, cleartrip is getting the information from Spicejet or Air India through an exposed API.
So if a user enters source/destination/date & searches a flight on 3rd party vendor (makemytrip.com), the vendor makes an API call to all the airlines and display the results in their respective website browser.
Another practical example would be, when we try to sign-up twitter.
Notice above that twitter gives us options to sign-up using gmail or with apple id. So basically, google and apple have exposed their login/sign-up APIs to twitter. So a user can login twitter using his gmail id (even though both gmail and twitter are different applications).
6. A very simple (raw) json
Below is a typical API json request (in raw form) that is sent by cleartrip to various airline companies
Below is a typical API json response received by cleartrip
This is how API helps in communicating between different softwares.x
7. Client-Server Architecture
There are 2 components in client-server architecture.
- Client is the consumer who wants some information from another software (a client can be a mobile phone, a desktop, laptop etc). Client sends HTTP request to server requesting some information;
- Server processes the request and responds back with HTTP response having the desired information
To setup the communication between client and server, we need a medium. This medium is HTTP protocol. Medium helps both the parties to communicate with each other.
Let us consider a simple analogy to understand this. Let us suppose your native language is Hindi and your friend’s native language is French. You would not understand anything in French and your friend would not understand anything in Hindi. So there should be a common medium, a common language (let’s suppose English) that both, you and your friend would understand. So this is the importance of medium.
- client sends the request in xml/json format
- server sends back the response in json/xml format
This helps the client-server to communicate with each other using a common data format (XML, JSON).
8. Difference between Webservice and API
The APIs that interact over HTTP protocol (need internet), we call them as webservices. For example, the cleartrip/makemytrip is a webservice, because, in order to display the search results, they need to interact over HTTP protocol (need Internet connection) with Indigo/AirIndia servers
The things that does not need internet or do not interact over HTTP protocol, we call them as API. For example, in order to work with Selenium, we need to integrate our java program with Selenium jar files. To interact with Selenium API jar files, we don’t need internet or http protocol. Similarly, to read data from excel, we can integrate POI library API with our java program and start working without any internet connection.
All webservices are APIs but reverse is not true. The weather app on your laptop/mobile is a webservice (api) that interacts with weather server and provides us real-time weather information.
In the next blog, we would discuss about the HTTP methods (Get, Post, Put, Delete), the components that make an HTTP request and HTTP response and the elements that constitute a REST request (URI etc).
Thank you for reading!