HTTP for Rails

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’

$ telnet example.com 80 $ GET / HTTP/1.1 $ Host: example.com

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)
  • 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(http://httpstatus.es)])
    • 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)
    • Content-Type: text/html Tells recipient how to parse the result)
    • 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)
    • Content-Length: 1270 (Content following this is 1270 bytes)
  • All this is followed by text that we know should be interpretted as text/html

What are the HTTP Methods

  • GET, PUT, POST, DELETE
  • Think of them like a reguired 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