Simple Note on HTTP Protocol

Salem Alqahtani
5 min readJun 15, 2022

--

This is my note on what HTTP is. This note was very helpful for me while I was coding HTTP in my project. I would like to share it with anybody who might find it helpful.

To start, HTTP stands for hypertext transfer protocol. Basically, it is a network protocol to transfer requests and responses between client and server nodes. In other words, HTTP is a standard way of exchanging data across an established connection.

For HTTP protocol to work, there are two things: establishing a connection and data exchange.

Each server should have an address known as an IP address. In order for the server to receive requests from different clients, the server should have ports listening for incoming requests. IP and ports are shared with the clients to ask for any kind of information that the server hosts.

Once the client has the IP and port for the server, a connection is established between the client and server. therefore the exchange of data between the server and client can begin. I will explain later in this post what I mean about establishing the connection.

The protocol defines two key types that the client and server can use when communicating and these are; request and response types.

HTTP Client Request: the client sends a Hypertext Transfer Protocol (HTTP) Request to the Hypertext Transfer Protocol (HTTP) Server according to the HTTP standard, specifying the information the client like to retrieve from the Hypertext Transfer Protocol (HTTP) Server.

HTTP Server Response: Once the Hypertext Transfer Protocol (HTTP) Request arrived at the Hypertext Transfer Protocol (HTTP) server, it will process the request and creates a Hypertext Transfer Protocol (HTTP) Response message. The Hypertext Transfer Protocol (HTTP) response message may contain the resource the Hypertext Transfer Protocol (HTTP) Client requested or information about why the Hypertext Transfer Protocol (HTTP) request failed.

Note: you should have the necessary packages that HTTP needs. The HTTP protocol should be on your machine. In Golang for instance, once you install the package, it automatically installs all HTTP and its dependencies on your machine. HTTP client is available on all browsers, but an HTTP server needs to be installed.

Now, you might wonder where HTTP is in the big picture of the network model. It is on the top close to the end-user. The below figure illustrates the network layers and HTTP role in the model.

The key term that you should remember is transferring data between nodes. One way is HTTP. From the figure above, HTTP is just one of many other protocols used to transfer data at the application layer level. others like FTP focus on file transfer.

To establish the connection, we need another protocol to help HTTP protocol to establish the connection. Here I will focus on TCP. The TCP protocol is the one that will establish the communication between two nodes. The figure below is very helpful to understand the role of TCP.

It is easy! nothing here is complicated. In terms of implementation, I do a toy example to demonstrate how the client-server HTTP protocol works in Golang.

package main

import (
"fmt"
"net/http"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world")
}
func main() {
http.HandleFunc("/", indexHandler)
// useshttp.HandleFuncAt root routing/Registered
http.ListenAndServe(":8000", nil)
}

If you hate HTTP and you want to handle the data in the application layer on your own, that is also possible. You need to forget about HTTP and start thinking of TCP Socket, which is an alternative solution for communication but not recommended.

Socket is an intermediate software abstraction layer for communication between the application layer and the TCP/IP protocol family. It is a group of interfaces. The socket is an abstract layer between the application layer and the transport layer. It abstracts the complex operation of the TCP/IP layer into several simple interfaces. It uses the layer to call the realized process to communicate in the network.

The sockets interface is not your best choice for a network programming API. Specifically, many excellent libraries exist to use higher-level protocols directly, without having to worry about the details of sockets — the libraries handle those details for you. In the big picture, you can see where socket API can fit in the network layer.

Now, I will get into more details about the Golang implementation. The common flow of HTTP is illustrated in the figure below.

Upon receiving the request from the client, the request enters the route first which is Multiplexer. The router finds a handler and builds a response for the request.

Go implementation is very simple. First, register the route, then create the service and enable listening. The code below demonstrates the get and post request in HTTP. It is very simple to follow.

That is just a simple note. HTTP is an extensible protocol that is easy to use. The client-server structure allows HTTP to advance the Web capabilities. In the next post, I will talk about the difference between HTTP and REST-API.

--

--