HTTP != REST
Very often, you read HTTP and REST together. Many think that HTTP and REST go hand-in-hand. But that is not true. HTTP is just a transfer protocol used in REST. You can write REST APIs using any transfer protocol, such as FTP, SNMP, SMTP etc.
Roy Fielding, who introduced REST principles to the world, has nowhere mentioned HTTP in his 6 guiding principles. You can read principles here #lesson2.
But why HTTP protocol is so popular in REST?
HTTP stands for Hyper Text Transfer Protocol. (A computer protocol is an accepted set of rules that govern the communication between two computers e.g. Bluetooth for connective devices.)⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀ The world-wide-web aka internet, runs on HTTP protocol. When you type http://google.com, your browser uses HTTP protocol to communicate with Google server to render the search results.
Below are 2 reasons why people use HTTP in REST:
- Ubiquitous nature of HTTP: Everyone knows HTTP and the infrastructure, servers & client libraries for HTTP are widely available
- Reduced learning curve: Since developers understand HTTP, the learning curve reduces significantly which encourages usage of APIs.
How HTTP works?
Communication in HTTP involves the exchange of request and response between client and server. Client sends a request to server to do something and server, in turn, responds whether it could do it or not.
HTTP Request
For a valid request, client needs 4 things:
- URL (Uniform Resource Locator)
- Method
- Header
- Body
URL (Uniform Resource Locator):
When you intend to access a webpage/videos/pdf, you type a specific URL in the browser and browser does rest of the things. In HTTP, everything which you access using URL is called resource. Every resource has a unique address which is its URL.
Method:
HTTP method tells the server which action user wants to perform on the resource.
The four methods most commonly used in APIs are:
- GET - Asks the server to retrieve a resource
- POST - Asks the server to create a new resource
- PUT - Asks the server to edit/update an existing resource
- DELETE - Asks the server to delete a resource
When you order a pizza from Dominos, you first search available pizza crusts. For this, your browser fires a GET request to fetch all available crusts.
Once you decide the order, you place an order using POST request. POST request tells server to create the order.
Later you realize that you forgot to add toppings. You go to the order and modify it using PUT request.
While waiting for the order, you make a bunch of GET requests to retrieve order status. After waiting for an hour, you get frustrated and cancel the order using DELETE method.
Header:
Header provides meta-information about the request such as source of the request, size of the request etc.
Have you ever wondered, how the same website gets rendered differently in desktop and mobile?
This is because when accessing the website, your browser sends the “User-Agent” in the header. This tells the server which device is accessing the website. Accordingly, server sends the optimized webpage.
Body:
This is the place where actual request details are exchanged between client and server. Continuing our Pizza example, body is where your order details such as crust type, toppings go to server.
Above 4 pieces make the complete HTTP request.
HTTP Response
After server receives the https request, it responds back with https response. HTTP response contains a similar structure as HTTP request but only difference is instead of Method and URL, it sends back Status Code. Rest part such as header and body follow the same structure as request.
Status Codes:
HTTP status codes are 3 digit numbers with unique meaning. There are 5 categories of status codes:
You might have seen below page which is famous 404 (Page not found)
When you try to access a page/resource which does not exist on server, server replies with “Page not found – 404”.
As mentioned in above table, there is a slew of other statuses such as 200 (“Success” – Request is good), 503 (“Our website is down”). HTTP status codes will be studied in depth in future articles.
After a response is delivered to the client, the Request-Response Cycle is completed and a round of communication is over. It is now up to the client to initiate any further interactions. The server will not send the client any data until it receives a new request
Below is HTTP response structure.
Let us Recap
HTTP and REST are disjoint concepts. HTTP is the protocol that supports a wide range of operations whereas REST is the guiding principle for APIs that is independent of any protocol. However, when you leverage HTTP functionalities, you can create a simple yet powerful REST APIs.
HTTP supports a wide range of operations to help client and server talk. A simple change in the request method from POST to DELETE can create a new resource or remove an existing resource. In addition, header values add flexibility in communication. Some APIs require a particular header, while others require specific information inside the request body. To get the desired result, you need to make the correct HTTP request.
Thanks for reading.