How to write raw HTTP requests using Netcat 🌐

A step towards better understanding of HTTP requests

How to write raw HTTP requests using Netcat 🌐

As a web developer, having a decent understanding of HTTP request methods, headers and status codes increases your overall productivity as a developer in the long run by helping you debug 🐞, develop and understand code faster⚡.

What better way to gain a better understanding of HTTP requests than writing them raw right from your terminal 🖥️.

In this blog, I'm going to help you write a raw basic HTTP GET request from your terminal using netcat in a beginner friendly way.

Let's begin by understanding what an HTTP request is!

start.gif


What is an HTTP request?

HTTP (Hyper Text Transfer Protocol) is the underlying format used to structure requests and responses for effective communication between a client and a server.

The message that is sent by a client to a server is known as an HTTP request.

We will be using example.com for analyzing and making HTTP requests in this blog.

If we go to example.com and switch to doc option in the networks tab in chrome devtools, we will see a HTTP GET request just like this one:

Request URL: https://example.com/
Request Method: GET
Status Code: 200 
Remote Address: [2606:2800:220:1:248:1893:25c8:1946]:443
Referrer Policy: strict-origin-when-cross-origin

You can read more about HTTP at MDN here.

Now, let's discuss what Netcat is.

What is Netcat?

loki.jpg

Netcat is a Unix utility which reads and writes data across network connections using TCP or UDP protocol.

To what we'll need, Netcat allows us to setup client-server connections from our terminal. It actually can do much more than that, but that is another topic in itself.

You can use Netcat in your terminal using the nc keyword in your commands.

If it went above your head, don't worry, implementation next.

Let's get to it, shall we?

In order to send off a request, first we need to setup a connection to the HTTP server.

To do that, we run the following command:

myPC:~$ nc example.com 80 -vvv

nc, as mentioned above, stands for netcat, next comes the domain and the server port number (80 is the default port number for HTTP connections).

The -vvv command turns on the verbose mode which prints additional information about the connection.

connection.png

Now that the connection is setup, we are all set to send off a GET HTTP Request to the server.

The request format requires three must things:

  • the request method (which is GET here)
  • the version (HTTP/1.1), and
  • the Host Domain (example.com)

Here's how it'll look:

request.png

Inorder to send the request, you just need to leave a line (press enter twice)

enter.gif

You can now see the response sent from the server, it has all the things the browser would need to display example.com 's webpage.

response.png

There you go, you wrote your own raw HTTP GET Request, would recommend you to study more about status code, different headers and all that from MDN, or just google it.


Thanks for reading it till now, do share if this helped you.

If you are into react, would recommend reading my blog on Creating a custom debounce react hook, it makes a great interview problem and also is widely used in daily use applications.

thankyou.gif