Open In App
Related Articles

Construct a simple HTTP request on TCP protocol

Like Article
Save Article
Report issue

HTTP Request :

  • HTTP messages are how data is exchanged between a server and a client. In this, there are two types of messages where one is HTTP client request and the second is the response from the server.
  • Messages in textual form and it is encoded in ASCII form, and span over multiple lines. And messages were openly sent across the connection in the case of HTTP/1.1 and earlier versions of the protocol. In HTTP/2, the once human-readable message is now divided up into HTTP frames, providing optimization and performance improvements.
  • Let’s now see the components of an HTTP request & response by actually creating one. The telnet client helps us connect to other computers on the internet. The format is telnet, hostname, and port.

Note – 

You can also use this online telnet client.

Opening a TCP connection to server via telnet

Steps to Construct a simple HTTP request on TCP protocol :

Step-1 :

The default port for HTTP is 80 and the telnet command has us connected to the HTTP port on the server. We can start sending HTTP requests to the server now.

Step-2 :

How do we create an HTTP request? Let’s check the HTTP protocol definition doc here to get an idea of how to frame an HTTP request.

Request :
    A request message from a client to server includes, within the 
    first line of that message, the method to be applied to the resource,
    the identifier of the resource, and the protocol version in use.
        Request    = Request-Line
                 *((  general-header
                 |    request-header
                 |    entity-header ) CRLF )
                [ message-body ]        
Request-Line :     
    The Request-Line begins with a method token, followed by the 
    Request-URL and the protocol version, and ending the CRLF.The
    elements are separated by SP characters. No CR or LF is allowed
    except in the final CRLF sequence.
        Request-Line = Method SP Request-URI SP HTTP_Version CRLF

HTTP Request specification :

Below given is the screenshot for your reference that shows the HTTP request specification.

HTTP Request-Response components :

  • From the above figure, different parts of the HTTP communication are:
  • Request Line (HTTP Request)
  • Status Line & Response Header (HTTP Response)
  • Response Body (HTTP Response)
  • Try to figure out what some of these response headers mean & what their uses are – for starters, see Last-Modified, Content-Length, Content-Type
  • If we analyze the network packets transferred to/from our computer during the above communication, we’ll be able to understand some things ( is the client computer &, the server)
  • The client initiates a TCP connection request to the server (Line 1) – this is performed when we execute the telnet command
  • HTTP’s communication happens using this established TCP connection (see the bottom part that lists outs the protocols used for the resource transfer)
  • The client sends the HTTP Request line to the server (Line 6) to which the server responds with the HTTP Status code & data as we saw earlier in the telnet output

Note – 

We can also analyze Network packets using Wire shark this will be done by you.

Last Updated : 01 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads