Nuts and Bolts: HTTP

INFO 253B: Backend Web Architecture

Kay Ashaolu

What resource to return?

  • How are resources specified?
  • How does servers know what data to send?

Hostname/Path -> Resource

  • http://localhost:5000/static/
  • How to specify hostname in HTTP?
  • How to specify path?
  • What method to use?
  • What do you return?

Let's look at a static webserver

from flask import Flask
import json

app = Flask(__name__,static_url_path="/static")

Let's look at a static webserver

  • Associate "/static/style.js" with
    • /Users/kay/Desktop/demo/static/style.js
  • Associate "/static/style.css" with
    • /Users/kay/Desktop/demo/static/style.css
  • Associate "/static/cool_img_1.png with
    • /Users/kay/Desktop/demo/static/cool_img_1.png
  • ​Associate "/static/cool_img_2.png with
    • /Users/kay/Desktop/demo/static/cool_img_2.png

Note: All requests to static servers are (typically) GET requests

Methods

  • GET
    • Requests data from a specified resource
  • POST
    • Submits data to be processed to a specified resource
  • PUT
    • Update specified resource
  • DELETE
    • Deletes specified resource

Missing File

  • What should we do if we request a file that is not there?
  • Return "Oops!"
  • Return nothing?
  • Return standard HTML?

HTTP Response Codes

  • Numbers are unambiguous, can be read by machines
  • Always return a response code
  • Return text as well for human readability

HTTP Response Codes

  • 200's: OK
  • 300's: Redirect
  • 400's: Client Errors
  • 500's: Server Errors

300's: Redirect

  • 301: Permanent Redirect
  • 302: Temporary Redirect
  • Location: Header with URI of target

400's: Client Errors

  • 401: Unauthorized
  • 404: Resource Not Found

500's: Server Errors

  • 500: Internal Server Error (Hard drive on fire)

Example

from flask import Flask, request
import json

app = Flask(__name__)

quote_db = {
  'sunday': "Life is about making an impact, not making an income. \
  –Kevin Kruse",
  'monday': "Whatever the mind of man can conceive and believe, it can achieve. \
  –Napoleon Hill",
  'tuesday': "Strive not to be a success, but rather to be of value. \
  –Albert Einstein",
  'wednesday': "You miss 100% of the shots you don’t take. \
  –Wayne Gretzky",
  'thursday': "Every strike brings me closer to the next home run. \
  –Babe Ruth",
  'friday': "We become what we think about. \
  –Earl Nightingale",
  'saturday': "Life is what happens to you while you’re busy making other plans. \
  –John Lennon",
}

def gen_response(day_of_week):
    if day_of_week.lower() not in quote_db:
        response = {"message": "Sorry we don't know that day of the week"}
        return json.dumps(response), 400

    response = {"day": day_of_week, "quote": quote_db[day_of_week.lower()]}
    return json.dumps(response), 200

Example

@app.route('/quote/<day_of_week>')
def quote_of_the_day(day_of_week):
  return gen_response(day_of_week)

@app.route('/quote', methods = ['GET'])
def quote_of_the_day_get():
  day_of_week = request.args.get("day_of_week")
  return gen_response(day_of_week)

@app.route('/quote', methods = ['POST'])
def quote_of_the_day_post():
  data = request.get_json()
  day_of_week = data["day_of_week"]
  return gen_response(day_of_week)

Response Headers

  • Gave metadata about request
  • Return metadata about response
  • Size, representation, language

Different Types of Resources

  • JSON
  • XML
  • HTML
  • CSS
  • JavaScript
  • Images
  • ...

Why have different protocols?

  • HTTP is about resource manipulation
  • Specifying what you want to do with a resource
  • How it is represented
  • Optimized for web browsing

Questions?