If you have an interest in web development and building APIs, understanding the concepts of HTTP is vital.

In this article you are going to learn the following:

πŸ€”What is HTTP?

HyperText Transfer Protocol (HTTP) is a stateless, connection-less, request-response protocol that consists of rules which define communications between clients and servers connected over an established Transmission Control Protocol (TCP) connection

Once there is an established connection between the client and the server, HTTP defines rules for transferring data such as images, text, and other multimedia between the client and the server

Before delving any further, let's define some basic terminologies which will be mentioned in the article.

πŸ“œ History of HTTP

Characteristics of HTTP

Although HTTP itself is stateless, there's a need to share state between multiple HTTP requests. HTTP cookies make it possible for the use of stateful sessions.

HTTP cookies are added to the header of each HTTP request allowing session creations on each HTTP request, doing this makes it possible to share state between multiple HTTP requests.

πŸ€”What are Sessions

A session is temporal storage on the server, each session consists of the session data and the session ID which identifies a specific session. The session data is stored on the server while the session ID is stored in the client(web browser) using HTTP cookies. To identify a specific session, the session ID is sent back to the server using HTTP cookies

Characteristics of Sessions

Sessions usage examples

πŸ”‚The HTTP process

To understand the HTTP process, let's ask ourselves, what actually happens each time a user types a URL into a browser in order to open a new webpage. Well, the most simple answer is:

This process is called the Request-Response cycle.

Let's take it one step further.

Let's say we want to access the explore page on hashnode.com by entering the following URL into the browser

The URL is made up of:

This process of DNS Lookup only occurs when the HTTP request is made.

When a client initiates an HTTP request to the server, it performs the following steps:

  1. It establishes a TCP connection between the client and server: the TCP connection is used to send requests and receive responses

  2. Once the TCP connection has been established, the client sends an HTTP request message to the server

  3. After interpreting an HTTP request message the server sends an HTTP response message to the client

  4. Once the server has responded to the client's request, the TCP connection is closed

This process is illustrated below.

πŸ“ HTTP messages

There are two types of HTTP messages:

HTTP Request Message

The request message consists of the following:

HTTP Response Message

The response message consists of the following:

HTTP Methods

HTTP uses methods to define the operation to be applied to the resource identified by the Request-URI. These methods are usually verbs, with the most widely used methods being POST, GET, PATCH, and DELETE. Each of these HTTP methods returns a success or failure upon completion of their operations, using status codes to represent success or a failure.

  1. GET: This method retrieves the resource specified in the request-URI. If successful the resource is returned as an entity in the response message along with the status code 200 (Success).
  2. POST: This method request that the server accepts the data or entity enclosed in the Request body, if successful the server responds by sending a response message containing the status code 201 (Created), along with the location of the posted entity or data.
  3. PUT: This method request that the enclosed data or entity in the Request body be used to modify the request identified by the Request-URI.
  4. DELETE: This method request that the enclosed data or entity in the request be stored under the supplied Request-URI.

πŸ“± HTTP Status Codes

The status codes are used by the client to determine if the HTTP request sent to the server, along with the specified operation was a success or a failure, remember the operation is specified by using the HTTP method.

The status code is a 3 digit result code, with the first digit defining the class of the code.

  1. 1xx (Informational): This class of status codes consists of informational responses indicating that a client should continue with a request. Examples of status codes belonging to this class;
    • 100 Continue: The client should continue with their request
    • 101 Switching protocols: The server is willing to comply with the request to change the application protocol being used in the connection, this request is made using the Upgrade message header field
  2. 2XX (Success): This class of status codes indicates that the request made by the client was successfully received, understood, and accepted by the server. Examples of status codes belonging to this class include the following;
    • 200 Ok: The request succeeded, the information returned with the response is dependent on the HTTP method used in the request.
      • GET: the requested resource is sent in the response
      • POST: A description of the action is sent in the response
    • 201 Created: The request has succeeded and a new resource has been created
    • 202 Accepted: The request has been accepted but not yet processed by the server
  3. 3XX (Redirection): This class of status codes indicates that further action needs to be taken by the user to complete the request, examples of status codes belonging to this class;
    • 301 Moved permanently: The requested resource has been moved permanently to a new URI, the permanent URI is given in the Location header field in the response
    • 304 Not modified: A successful get request is made, but the client does not modify the resource
  4. 4XX (Client Error): This class of status code indicates that the client has erred. Examples of status codes belonging to this class;
    • 400 Bad requests: The request could not be understood by the server
    • 401 Unauthorized: The request requires user authentication
    • 404 Not found: The server can not find any resource matching the resource-URI
  5. 5XX (Server Error): This class of status code indicates that the server has erred. Examples of status codes belonging to this class;
    • 500 Internal error: The server encountered an unexpected condition with prevented it from fulfilling the request
    • 503 Service unavailable: The server is unable to handle the request due to a temporal condition such as server maintenance or temporal overloading

You can read a lot more about status codes here

Conclusion

I hope you found this article useful and helpful, if you have any questions let me know in the comments. In a later article, we are going to learn how to implement and use these concepts in building a Rest API. That's it for now πŸ‘‹

Also published on: https://azamahjr.hashnode.dev/http-simplified