Construct a simple HTTP request on TCP protocol
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.
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 :
The default port for HTTP is 80 and the telnet command has us connected to the HTTP port on the geeksforgeeks.org server. We can start sending HTTP requests to the server now.
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 *(( geneal-header | request-header | entity-header ) CRLF ) 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 seperated 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 (192.168.43.197 is the client computer & 126.96.36.199, 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
We can also analyze Network packets using Wire shark this will be done by you.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.