HTTP & Headers

In notebook:
FrontEndMasters Networking and Streams
Created at:
2017-09-23
Updated:
2017-09-30
Tags:
backend Fundamentals

hypertext transfer protocol

It's an easy protocol to type out. It's fairly readable and tolerant text based commands.

http verbs

HTTP requests begin with a VERB. Here are some things each VERB is used for:

  • GET - fetch a document
  • POST - submit a form
  • HEAD - fetch metadata about a document
  • PUT - upload a file

These are typical examples, of how you would them. For example POST is often used to upload a file, because of network proxies that won't allow PUT. You also have DELETE etc. that are less used.


http headers

Next come the headers.

Headers have a key followed by a colon followed by a value:

key: value

So for example, this would look like this

http

using netcat:

$ nc google.com 80
GET / HTTP/1.0
Host: google.com

So we have the Host: header. It tells what domain you want to access. A server can host multiple domains or a load balancer scenario.

Make sure to hit return twice after Host: google.com. This is how the server knows that you are done You can have multiple headers and then you close it by two newlines to tell the server you are done.

Then you get back your response:

The body is separated from the headers by an empty line:

HTTP/1.0 301 Moved Permanently
Location: http://www.google.com/  ☛ just set up like this
Content-Type: text/html; charset=UTF-8 ☛ type of content (html)
Date: Mon, 12 Jan 2015 01:26:19 GMT ☛ for cache control (When to fetch new version)
Expires: Wed, 11 Feb 2015 01:26:19 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219 ☛ tells you how long the body will be
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic,p=0.02

<HTML><HEAD><meta http-equiv="content-type"
content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

301 moves permanent move...


if you want to fetch other types of documents

$ nc substack.net 80
GET /favicon.ico HTTP/1.0
Host: substack.net

☛ double enter
reply...

(he gets a 404...)