In the previous post, I explained the role of API for client and server, this article will talk about API Protocol – the way how those two guys talk to each other (roughly called 2 computers).
What is API protocol ?
Suppose: There are 2 people A and B talking to each other on the phone, if person A asks a question and then stays silent, person B will know that person A is waiting for an answer and it is person B’s turn to speak. The two computers also communicate politely and are described with the term “Protocol”.
API protocol are the accepted rules for two computers to talk to.
However, this rule is much stricter than human-to-human communication. The computer will not be smart to be able to recognize that two sentences “A is husband B” or “B is wife A” have the same meaning. For two computers to communicate effectively, the server must know exactly how the client arranges the message it sends.
We have heard of Protocols for different purposes, such as Mail with POP or IMAP, messages with XMPP, Device connection: Bluetooth. In the web, the main Protocol is HTTP – HyperText Transfer Protocol, and it is the protocol used for APIs.
How does HTTP works ?
The life of HTTP revolves around a vicious cycle: Request and Response. The client sends the request, the server sends back a response as to whether the server can do what the client wants or not. And the API is built on two main components: Request and Response. First, we must understand the structure of each component.
A standard request requires 4 things:
OK, now look at each of them one by one.
- An URL is a unique address for something (using a noun), be it a web page, image, or video. The API extends the original idea of URLs to other things, e.g. customers, products. And so the client easily tells the server what it wants, these are also known collectively as “resources”.
- Method: is the action the client wants to act on “resources”, and it is usually a verb. There are four commonly used methods:
– GET: Ask the server to return resources: Imagine the scene on fb, swiping new feeds.
– POST: Ask the server to create a new resource. For example, register for a ride at bike app.
– PUT: Ask the server to edit / add resources already on the system. Example: Edit 1 post on fb.
– DELETE: Ask the server to delete a resource. This probably doesn’t need an example.
- Headers: where contains the necessary information of a request but end-users do not know its existence. For example, the length of the request body, the time it took to send the request, the type of device in use, the type of response format that the client can read…
- Body: where the client will fill in the information. Suppose you order a pizza, the information in the body will be: Type of pizza, size, quantity ordered.
After receiving a request from the client, the server will process that request and send a response back to the client. The structure of a response is quite similar to the request part, but the Status code will replace the URL and Method. In a nutshell, it has a 3-part structure:
1. Status code
Status codes are 3-digit numbers with only 1 meaning. Surely you are no longer strange to the Error “404 Not Found” or “503 Service Unavailable”. Full list is available here.
Headers and Body are similar to request.
Thank you for reading.
To see more articles about API testing , please go this link