HTTP - Hypertext Transfer Protocol
HTTP is a network protocol
- Mental model: calling a function across a network
- Client is the caller
- Server is where the function exists
- Client receives the result
Let’s use telnet to make such a ‘call’
<%= source_begin “language-bash” %>
$ telnet example.com 80
$ GET / HTTP/1.1
$ Host: example.com
<%= source_end %>
Review: What does HTTP do?
- Expects a “request” message which is answered by a “response”
- Request message
- Defines a “method”
- Defines the “path”
- Supplies information about the format and lots of other stuff
- Response message
- Defines a status code
- Defines format of the response
- Defines the response body
Deconstructing the call
- “Call” includes:
- target host (example.com)
- target port on the host (80)
- HTTP verb: “GET” (a.k.a. method) (ref: HTTP Method Definitions)
- address: (the path or “/”)
- Version of HTTP protocol (1.1)
1.Datatype of required response (aka mime-type)
- There are lots of other “parameters” that are allowed in this call
Deconstructing the returned data from that call
- (Reference: HTTP Response Fields)
- “Response” includes:
- Status code: 200 (see HTTP Status Codes)
- Content-Type: text/html Tells recipient how to parse the result)
- Content-Length: 1270 (Content following this is 1270 bytes)
- There are lots of other “parameters” that are allowed in this call
- Accept-Ranges: bytes (lets the server tell the caller that it has that capability)
- Cache-Control: max-age=604800, how long this response may be cached)
- Date: Thu, 27 Feb 2014 00:33:26 GMT The current date time on the server)
- Etag: “359670651” kind of a unique-id for caching purposes)
- Expires: Thu, 06 Mar 2014 00:33:26 GMT (says when the page is required to expire)
- Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT (When this page was edited)
- Server: ECS (ewr/1584) (Software running the server)
- X-Cache: HIT (Page came out of a cache)
- x-ec-custom-error: 1 (There was no error in the cache)
- All this is followed by data that we know should be interpreted as the Content-Type.
What are the HTTP Methods
- GET, PUT, POST, DELETE (there are more, those are the most common ones)
- Think of them like a required first argument to the call
- The interpretation of the
Simplified view of the world
- Think of the relationship between the client (the user in a browser) and the server (the rails server) as a program calling a function!
- Call: CallUsingHTTP(“GET”, “www.brandeis.edu”, “/index.html”)
- Returns: Status code, Content type, Content, and potentially more