From the course: Build REST APIs with FastAPI

The HTTP protocol

REST APIs use the HTTP protocol to transfer messages. HTTP is another acronym. It stands for HyperText Transfer Protocol. It is a textual-based protocol that runs on sockets. Let's see how it looks behind the scenes. So here I'm using the Netcat utility that works with raw sockets. I'm accessing httpbin.org at Port 80, which is the default HTTP port. And I'm sending an HTTP request. The request is from line four to line 14. The first one is the request line and this is POST. This is the verb that I'm asking. Then the path I'm accessing. And this is /post because httpbin.org is a generic utility on your own server. It will probably slash events or slash logs or something like that. And then what version of HTTP I'm talking, which is 1.1. After the first line, we have few header lines, and headers are the header names, colon, and then the value. And the host header is the only require header in requests. And we're saying we're accessing httpbin.org. We're telling the server to close the connection when it's done sending us data, we're telling it we're sending JSON data, and there are going to be 75 bytes of this data. After that, we have an empty line and then starts the body of the HTTP request. And 75 bytes are for that body only, does not include the header or the first line. So if I'm going to do 01_02 and the POST, I'm going to see a request from the server. Let me make it bigger. So we see now that the server is returning. It says I'm returning HTTP/1.1, I'm returning a status code 200, which is okay. And there is a reason which is the okay. And if it's a message like 404, not found, and other status codes. After that, again, we have the sequence of headers. Some of them are important like what the server is sending, application JSON, how many bytes it's sending. And some of them are informational. This is the server. And some of them are for the browser. These are for security. Then again we have an empty line and after that, we have the body of the response. This is a JSON response. And we see that in the JSON part, we have the same thing that we sent to the server.

Contents