Trading web pages: HTTP
Type an address in the bar and how does that page appear on screen? Out of sight, a short conversation goes back and forth. The browser asks, "give me this," and the server hands it over, "here you go." This pair, the ask and the hand-over, is the promise that runs the web: HTTP.
Give me this, here you go
Earlier, thanks to TCP,
we saw data arrive with nothing missing.
But what conversation
do we hold using that data?
The conversation on the web is very simple.
The browser asks the server,
"give me this page,"
and the server hands it over, "here you go."
The asking is a request, the handing-over a response.
Tap the browser to send a request to the server. When the server returns a response, the pair is complete.
One request, one response.
The web runs on this simple pairing.
The browser speaks first,
and the server answers only that ask.
The server never speaks up first.
There is always an asker and an answerer.
But besides "give me this,"
there are more kinds of asking.
Next, let's look at those actions.
The action inside a request
Every request carries an action,
saying what it wants to do.
The most common is GET.
It means "please give me this," fetching something back.
GET is what you use each time a page opens.
On the other side is POST.
It means "please take this," sending something out.
Like when you post a comment or send login info.
Fetching is GET, sending is POST.
Tap an action to pick it. GET fetches, POST sends. An arrow shows which way each action points.
With just GET and POST,
most of what we do on the web is explained.
Opening a page to read is GET,
writing something to send is POST.
Besides the action, a short mark rides along with the response.
If all went well, 200,
if no such page exists, a number like 404.
It tells at a glance whether it succeeded or failed.
The details you need not know right now.
Like a first-time guest, every time
Here is HTTP's odd character.
HTTP is terribly forgetful.
The moment one request ends,
it utterly forgets who just asked what.
Even if the same person comes twice,
HTTP treats them like a first-time guest every time.
There is no memory of "weren't you here a moment ago?"
This trait of not holding on to the past
is called having no state, that is, stateless.
Send a request twice as the same person. The second time the server still greets you like a stranger. HTTP cannot remember the past.
Greeting you anew even on a second visit
does look a bit frustrating.
But this was made that way on purpose.
Since the server treats everyone the same, lightly,
it can handle a great many guests at once.
In exchange, to remember "that person from before"
you must hand them a separate ticket.
That ticket is what we meet next lesson:
sessions and cookies.
Gathering pieces into one page
One page does not all arrive
in a single request.
First you GET the skeleton of the text.
Looking inside, it says it also needs images and styling.
So the browser asks again,
give me the image, give me the styling.
Trading request and response several times,
it gathers the pieces one by one
until at last it becomes a finished page.
Tap each piece to request it. Text, image, and styling come back as responses, and the page fills in step by step.
With all the pieces gathered,
the empty spots are now full.
To our eyes it seems to pop up at once,
but it is really many request-responses combined.
One skeleton, one image, one bit of styling.
Each asked for separately and fetched separately.
Thanks to a fast internet,
all of this finishes in the blink of an eye,
so we simply feel the page appeared.
Let's wrap up
Gathered on one line, it's this.
The web moves in request-and-response pairs.
A request carries an action like GET (please give) or POST (please take).
One page is made by gathering pieces over many request-responses.
And HTTP is terribly forgetful,
so it treats every request like a first-time guest.
Thanks to this simplicity and forgetfulness,
the web stays light and easy for anyone to use.
That is HTTP's promise.
Tap the key points in order to review. (request and response → GET/POST actions → terribly forgetful → gather pieces into a page)
Now you know how web pages
go back and forth.
But HTTP's forgetfulness
left us one piece of homework.
The items in your cart,
or your logged-in state, how are they remembered?
If you are a first-time guest every time,
how do we carry that across?
Let's unlock that secret next lesson,
in sessions and cookies.