HTTP & REST - mattoattacko/class GitHub Wiki

Overview

  • HTTP aka "hypertext transfer protocol".
  • HTTP is a stateless, application-layer protocol for communicating between distributed systems.
  • HTTP lets us communicate between many different hosts and clients, supporting multiple network configs.
  • By default is set up on TCP/IP port 80.

But How?

  • Communication between hosts and clients occurs via the request/response cycle (or r/r pair).
  • Our client initiates an HTTP request. That request is serviced through an HTTP response message.
  • Request messages are sent via Uniform Resource Locators (URL).
  • The URL is structured as such:
  1. http = protocol
  2. www.blabla.com = the host
  3. .com:1234 = port
  4. /stuff/stuff/stuff = resource path or local path
  5. ?a=b&c=d = query
  • URLs tell us who the specific host we are trying to communicate with actually is.

Vocabulary & Syntax

  • There are four verbs that we will mainly run across when working with requests; GET, POST, PUT, DELETE.
  • GET: this is how we are fetching an existing resource. URLs have all the information our server needs to find and return the resource. _HEAD _is like GET, but does not have the message body. HEAD retrieves the server headers to see if the resource has changed.
  • POST: how we create a new resource. Our POST requests will have a "payload" that tells us what the data is for the new resource.
  • PUT: this is how we update an existing resource. The payload here might contain any updated data for the resource.
  • DELETE: this is how we delete an existing resource.
  • HTTP also supports **TRACE **(used to retrieve the hops that a request takes to and from the server. Useful for diagnostics) and **OPTIONS **(used to retrieve the server capabilities).

Status Codes

  • Status codes tell us how to interpret the server response.
  • 1xx Informational Messages: purely provisional
  • 2xx Successful: tells us that our request was successfully processed.
  • 3xx Redirection: tells us that our client needs to take additional action, usually used to jump to a different URL to get the resource.
  • 4xx Client Error: tells us that the server thinks we did something wrong. We either requested an invalid resource or made a bad request. Most commonly seen with 404s.
  • 5xx Server Error: tells us that there was a server failure while processing our request. Usually thrown as a 500 internal server error.

Headers

  • Can be broken down into General, Request Specific, Response Specific, and Entity.
  • General Headers: shared by request/response messages
  • Entity Headers: provides us with meta information about the content of the page.
  • Request Specific Headers: has the same structure as the others, but adds a request line.
  • Response Specific Headers: like request message, except has status line and headers.