We just started learning Sinatra this week, and tomorrow we’ll be given our midterm projects and groups. Eek!
Today we learned about cookies! (not the edible type) An HTTP cookie, also called a browser cookie is a small piece of data that is sent from a website’s server and is stored on a user’s browser.
1. When a user agent (web browser) first sends an HTTP request to the server, it does not yet have a cookie (session id).
2. The server sends an HTTP response back with a set-cookie in the response header. This is the server telling the browser “Hey you! I want you to send this piece of data to me every time you make a request from now on.”
3. The user agent sets the cookie when it receives the response. When the user agent sends another request to the server, it will have the cookie in its request header.
4. The server looks at the cookie, and ‘knows’ that this request is coming from the same place the first one came from.
Now, when you surf in incognito mode, you may think “no cookies ever”! But the reality is, you just don’t have any cookies in your request header to start with. Once the browser sends a request, the server response is going to have a set-cookie. Then you’re going to have a cookie in all later requests you send.
Cookies are great. Because HTTP is stateless and cookies help us mimic ‘persistence’ in our web apps!
And they taste good.