Let's write an HTTP Client in Rust: Part 1
In this post, we are going to learn what is HTTP, and how we can write an HTTP Client in Rust programming language from scratch. Of course, After this post, you can write a client in any programming language you want. Let's begin, Shall we?
What is HTTP?
HTTP(Hypertext Transfer Protocol) is a text-based Protocol built on top of TCP(Transmission Control Protocol), layer 4 of the OSI model. HTTP has 5 versions:
- HTTP 0.9
- HTTP 1.0
- HTTP 1.1
- HTTP 2
- HTTP 3
The HTTP/0.9 is the first version and it is versioned due to separating it from newer versions. The important versions are HTTP/1.0, HTTP/1.1, and HTTP/2. Anyway, We aren't here to talk about HTTP versions.
How HTTP works?
Basically, we create a REQUEST with a specific METHOD type and get a RESPONSE based on our request. We have a few request methods:
- GET
- POST
- HEAD
- OPTIONS
- PUT
- PATCH
Below, You can see the Scheme of an HTTP request:
The Important difference between HTTP/1.0 and HTTP/1.1 is in version 1.1, the connection remains open for reuse and If we want to close it, we have to specify it with a specific header.
What is\r\n
? and why we use it. this is the famous CRLF which is an abbreviation forCarriage Return Line Feed
. It is a sign for a new line. there you go, search about it.
This is a full example of an HTTP POST request to benyaamin.com/create/new
for creating a post:
Just POST
request has a body on it, So let's see what a request without a body looks like:
As I said earlier, HTTP is a text-based protocol so That's it.
In the next part, I'm going to write about how to send a request in code.
You can watch my Youtube Playlist for this article.