Overview
This article talks about the most common yet overlooked thing in web development- HTTP or Hypertext Transfer Protocol. We will go through what is HTTP, why we need it and how we use it one by one.What is HTTP?
We are aware of how the web works. We know that there is a Server and a Client which communicate with each other to exchange information. Okay, but how does this Communication take place? Surely, there has to be some way for the server to know what the client wants and where the client is. Well, this communication takes place in the form of "Requests and Responses". The Client sends a Request to the server, specifying it's requirement (user’s requirement), The server analyses the request and generates a Response, which may be in the form of the requested resource- file, image, video etc. or an error message.
"HTTP stands for "Hyper Text Transfer Protocol". It is a protocol used on the WWW to fetch or send resources to or from the server. It sits at the top of the protocol stack, in the Application Layer"
Let's take an example: When you type www.abc.com in your browser search box, the browser sends an HTTP Request to the server, asking the server to send back an HTML document(the webpage of abc.com). So, here HTML page is the requested resource. Come to the server, the server parses the request, and looks for the HTML page on it's storage- Suppose, the HTML page was found, in this case, it will send it along with STATUS Code of SUCCESS back to the Client. This is called Response. Now, suppose it was unable to find the document due to some reason- may be there is a server fault, or the file does not exist on the server etc. , In this case, it sends an ERROR CODE as response.
Now, we'll look at some of the features of HTTP:-
-
HTTP is Stateless
We say HTTP is Stateless or Connection-less because each HTTP request executes independently i.e. without any information related to the previous request. example- Suppose, you search for "www.abc.com" from the same browser in two different tabs. Both the tabs would load a page sent by the Server. Both these page loads would be totally independent and fresh, they won't relate to the other page in the different tab. -
HTTP is not session-less
HTTP is Stateless but not session-less. This means, if a website stores some session information in your browser's session storage, each HTTP response would utilize the same session information, until the session expires. -
HTTP connection
HTTP uses the standard TCP Protocol for connection. TCP is found comes in the protocol stack, just after the Application layer. TCP is reliable and connection oriented, hence the transmission of requests or packets through HTTP is secure and undisturbed. Protocol stack has been discussed in more detail in this post. -
HTTP is more readable and easy-to-understand
HTTP has a clear and concise structure of transmitting data in the form of headers, unlike some XML, which consists of angular brackets to declare tags.
HTTP Header
HTTP Headers are used to pass additional information along with the HTTP request or response. They consist of property names, followed by a colon and the property value.HTTP Request
An HTTP Request is an HTTP Message which is made to the host server, in order to fetch resources in the form of Responses( which are sent by the server).Components of a Request
We'll consider a request which fetches a webpage "home.html" from the host server "abc234.com" (this is just an imaginary name). Request comprises of the following:-
Request method
You need to specify an HTTP Method in at the beginning of request, defining the kind of operation you want to perform. For e.g. if you want to fetch a webpage from the server, you'll use the GET method. Various HTTP methods would be discussed in the later section. -
Path
Specify the path of the resource on the server, basically specify the name of the resource on the server along with the path on server. Omit the other parts of the URL i.e "http://" , domain name(abc234.com). For this request, our path would be "/home.html". -
Version of HTTP
There are two dominant versions of HTTP: HTTP 1.1 & HTTP 2.0. HTTP 1.1 being the more widely used. HTTP 2.0 may dominate in the future, as it is the more advanced version of HTTP. A brief about the difference between the two would be that HTTP 2.0 handles multiple requests on a single TCP connection, while HTTP 1.0 requires a separate connection for each request. So, accordingly you need to specify an HTTP version in the request. -
Header information
As mentioned earlier, Headers are used to provide some additional information regarding the request. Let's say we want the response which is not more than 10 seconds old, we can add a "max-age" header attribute in our request. You can specify any other optional headers you want. -
Request Body
: In case of methods like POST, we need to send some data to the server, this data is stored in the body. For example- you want to fetch all books starting from the letter "A" from the server, a typical body could be something like-
{ "query":"A", "count":10 }
Components of HTTP Response
-
HTTP Version
Just like we saw in the Request. This is the version of HTTP protocol. -
HTTP Status Code
A status code indicates the result of the request as processed by the server. It could be in the range of successful Status codes, error codes, informational codes, redirection codes and more. Some of the common status codes are tabulated below-Status Code Indication 200 OK Status OK. Request returned a successful response. 201 CREATED A resource has been created successfully by the server. 202 ACCEPTED The request was successfully accepted but not yet processed by the server. It is waiting for a batch job to finish for example. 301 MOVED PERMANENTLY The requested resource is no longer available on the requested URL. 304 NOT MODIFIED The response has not been modified. This means that the cached version of the response can be used. 400 BAD REQUEST The server could not understand the request syntax. Hence, the request could not be processed. 401 UNAUTHORIZED The "Authorization" header was missing in the request, which is required to authenticate the user. 403 FORBIDDEN The "Authorization" header was present but the server evaluated that the client is not allowed to access that particular resource. 404 NOT FOUND The requested resource was not found. 500 INTERNAL SERVER ERROR The server encountered an unhandled error while trying to process the request. 502 BAD GATEWAY The server encountered an error while trying to fetch a response from another server. Could also indicate server overloads & network problems. 503 SERVICE UNAVAILABLE The server is currently not in a state to handle the request.
This was a basic introduction to HTTP and its methods. If you understood so far, this should be enough to get you started with implementing HTTP logic in your servers.