HTTP Protocol
In the Client Server Architecture, we discussed HTTP requests and HTTP responses. Now, let us try to understand what is the meaning of HTTP.
-
When the client wants to communicate with the server, it needs to establish a TCP connection.
-
To establish a TCP connection, the client and server need to follow a three-way handshake model.
-
When the TCP connection is established, the client can send an HTTP request.
-
HTTP stands for Hyper Text Transfer Protocol.
-
It is a protocol used by the client to send a request to the server.
HTTP Request and HTTP Response
Now, you have learned what is HTTP, let us try to understand what HTTP Request and HTTP Response contains.
-
Every HTTP request contains three elements and they are- Request Line, Request Header, and Request Body.
-
Request Line specifies the method like the GET method, and POST method, which tells the server what to do with the information or resource. You will learn more about these HTTP methods later in this video.
-
Request line also contains the URL of the request which is used to find the resource on the server. For example the URL "www.example.com/video/Vaccation.mp4" is requesting a video file having the name Vaccation.mp4
-
Similarly, request line also specifies the HTTP protocol version being used. For Ex. HTTP/ 1.0, HTTP/1.1, and HTTP 2.
-
Request Headers are used to pass more information about the request, so that, using the request headers the server knows how to deal with the information the client is demanding. It contains information like cookies, connection, languages, content-length, cache and many more.
-
In the request body, we pass the data that is required by the server. For example, you need to pass a username and password in the request body while performing sign in in any website.
-
Similarly, an HTTP response contains a status line, headers, and body.
-
Status line is the first line in the response message. It consists of three items:
- The HTTP protocol version that is being used.
- A status code, which is a three-digit number that indicates the result of the request.
- A status text, which is human-readable text that summarizes the meaning of the status code.
-
Response headers contain information that a client can use to find out more about the response, and about the server like content-type, content encoding, set-cookie, connection, and many more.
-
Response body contains information about the status of the action which is requested by the client or the resource which is requested by the client.
HTTP Methods
Now, you have learnt about HTTP request and HTTP response, lets discuss about the HTTP methods.
-
We have learnt that HTTP request contains request line and in request line we need to specify the HTTP methods that tells the server what to do with the information or resource.
-
Generally, there are five HTTP methods which are commonly used and they are GET, POST, PUT, PATCH, DELETE.
-
GET method is used to fetch a resource from the server. For example, if you want to see your profile information, you will need to send a GET request to the server.
-
POST method is used to create a resource in the server. For example, if you want to create a new account, then you will fill the signup form and on hitting the signup button, you will send a post request to the server.
-
PUT method is used to update a certain resource from the server. For example, if you want to update your personal information like phone number, email, then you need to send PUT request to the server.
-
PATCH method is used for partial update. For example, if there is a resource where the server has stored name, date of birth, phone number, email & address informations, and if you need to update only one field, then you need to use PATCH method in the HTTP request. Using PATCH method, you only need to specify the field which is required to be updated, while all the other fields can be ignored in the HTTP request.
-
DELETE method is used to delete a resource from the server. For example, if you want to delete your profile from the server, then you need to use DELETE method in the HTTP request.
-
HTTP Status Code
Now, you have learned about what is HTTP protocol, HTTP request, HTTP response, and HTTP methods, it's time to learn about HTTP status codes.
-
When the client sends an HTTP request to the server, the server returns an HTTP response. Every HTTP response has some HTTP status code. The HTTP status code is a three-digit number that indicates the result of the request.
-
The HTTP Status code is divided into 5 different classes.
-
The first class is called Informational Class whose status code starts with 1. Under this class, we have some status codes like 100, and 101.
- 100 status code is used to tell that the client should continue the request or ignore the response if the request is already finished.
- 101 status code is sent in response to an Upgrade request header from the client and indicates the protocol the server is switching to.
-
The second class is called Success Class whose status code starts with 2. Under this class, we have some status codes like 200, 201, 202, 203, 204, 205, 206, and many more. The most important status code in this class is 200, 201, and 202.
- 200 status code denotes that the request is successful.
- 201 status code denotes that a new resource has been created.
- 202 status code denotes that the request is accepted but it is not immediately acted upon.
-
The third class is called Redirection Class whose status code starts with 3. Under this class, we have some status codes like 300, 301, 302, 303, 304, 305, 306, 307, and 308. The most important status code in this class is 301, 302, and 304.
- 301 status code denotes that the requested URL is no longer used by the server.
- 302 status code denotes that the requested URL has been removed temporarily.
- 304 status code is used for caching purposes. It tells the client that the response has not been modified, so the client can continue to use the same cached version of the response.
-
The fourth class is called Client Error Class whose status code starts with 4. Under this class, we have some status codes like 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, and many more. The most important status code in this class is 400, 401, 403, and 404.
- 400 status code is called a Bad request which means that there is an error in the request.
- 401 status code is called Unauthorized which means that the request lacks proper authorization.
- 403 status code is called Forbidden which means that the service is denied because the client doesn't have proper rights to access the resource.
- 404 status code is called Not Found which means that the resource is not found in the server.
-
The last and fifth class is called the Server Error class whose status code starts with 5. Under this class, we have some status codes like 500, 501, 502, 503, 504, 505, and 506. The most important status code in this class is 500, 501, and 503.
- 500 status code is called Internal Server Error which means that there is an error on the server side.
- 503 status code is called Service Unavailable Error which means that the service is temporarily unavailable but may be requested in the future.
- In this way, we have learned about different HTTP status codes, and you don't need to remember all of these status code. Once you start building the projects, you will automatically remember these status codes.
- For now, you just need to understand the fundamental concepts so that if someone asks you what is HTTP, the requests and responses, the HTTP methods, and the status codes, you should be able to say something about it.