I’m trying to write a server/API that interacts with client side API. To do that, I need to learn HTTP in depth. These are my notes from doing a deep dive into how HTTP works.
I wish there was a good tutorial where someone teaches you how to write a bunch of server integrations using HTTP requests quickly. I love to learn using muscle memory, but information related to HTTP is always very technical and dense.
My goal is to write a web app where server side Express code speaks with a SurveyJS based client side front-end efficiently.
Learning HTTP is an important step to achieving that.
WebDev Cave
It was helpful to watch this video closely before moving onto the more dense TutorialsPoint HTTP tutorial. It’s a bit boring, but we learn some important things and it preps the learner to go deeper. Specific valuable notes include:
- HTTP is the most used protocol in the world
- HTTP = The internet’s Postal Service
- TCP/IP based protocol
- To me, this means HTTP is just a checklist for communication across the internet
- Connectionless protocol – The request for information and the response are not part of a single connection. Each request and response is a fresh connection between client and server.
- The most reliable way to move data on the web
Request – Response Cycle
The user generates a request (HTTP Message). The request is sent to the server. The client then disconnects and waits for a response.
The server processes the request, prepares the response, establishes the connection again with the user and sends back the response in the form of a HTTP message. The server disconnects after the HTTP message is sent.
What an HTTP Message Looks Like
HTTP Request
Start line –
- METHOD – describes what to do.
- GET | POST |DELTE | UPDATE (See below for what these all do)
- URI – This describes where the resources we want are are.
- Version – This tells us what version of HTTP to Use (e.g. HTTP/1.0)
Header – Key value pairs that describes language, host and the media type we are using. For example:
- Host = www.example.com
- Accept-language = en-us
- Accept: text/html (image/gif, text/html etc…)
HTTP Response
Start line –
- Version – This tells us what version of HTTP to Use (e.g. HTTP/1.0)
- Status Code – How the connection faired. See below for a more in depth description of status codes. (e.g. 404 = fail or 200 = success)
Header – Key value pairs that describes language, host and the media type we are using. For example:
- Host = www.example.com
- Accept-language = en-us
- Accept: text/html (image/gif, text/html etc…)
Body – A path to where the response data is. An example would be __dirname + /events
Tutorials Point HTTP
This HTTP tutorial is very helpful because it lays out the specifics of the technology. HTTP is actually quite simple.
HTTP Status Codes
Status codes always confused me, though after reading the tutorial, they make a lot more sense. Here’s how they break down.
- 1xx: Informational – Status codes that begin with 1 tell the developer that things are fine and the code is just providing us information.
- 2xx: Success – Status codes that begin with 2 tell the developer that something has been accepted, received or understood.
- 3xx: Redirection – Status codes that begin with 3 tell the developer that further action needs to be taken before the request can be completed
- 4xx: Client Error – Status codes that begin with 4 tell the developer/user that the request is written incorrectly or that the request cannot be fulfilled.
- 5xx: Server Error – Status codes that begin with 5 tell the developer/user that the server failed to do something, despite the request being a valid one.
HTTP Methods
- GET – This retrieves information from a server based on the given URI. This should only retrieve data and have no effect on the data stored within the server
- HEAD – This is GET but without the entity-body.
- POST – Used when the client wants to send data to the server.
- PUT – Requests the server stores something included in the HTTP request. The server stores the data and sends a response back tot he client saying the data has been created.
- DELETE – Requests the server deletes a file at a specific location based on the URL. The server will delete the file and will then send a response to confirm deletion.
- CONNECT – Used to establish a connection to a server. When the client does this, they receive a response based on the current configuration.
- TRACE – This echos the contents of the HTTP request back to the requester. This is used for debugging purposes during development. When the server processes the request, it will send a message in response to the request.
Conclusion-ish
Ok so HTTP requests are not that complex. It’s just a computer system of interaction where the process of information is exact singular. Make a request, close communication. Create a response, close communication.
The part I’m really struggling with is how the HTTP requests are sent around using ExpressJS. That’s what I’ll focus on tomorrow.
Thank you for reading. Leave a comment below if you have some quality insights I’ve missed here. If you found this helpful, please let me know below as well.