Giter VIP home page Giter VIP logo

rack-status-codes-readme's Introduction

HTTP Status Codes

Objectives

  1. Define status codes and what they communicate to a client
  2. Describe the structure and various categories of status codes
  3. Set a response's status code in Rack

Why Status Codes are Important for the Client

Status codes allow your server to tell something special to the client. The responses you send need to be effective to both a human user and to the browser itself. That means that response messages like File Not Found or Item isn't in the cart work if there is a human to read the English. Browsers also want to know the status of the response. To get that response, the HTTP protocol has an agreed upon contract for different "status codes". A status code is a 3-digit integer where the first digit represents the class of the response, and the remaining two digits represent a specific status. There are 5 primary values that the first digit can take.

Status Code Chart

Status Number Code/Description
1 1xx: Informational (request received and continuing process)
2 2xx: Success (request successfully received, understood, and accepted)
3 3xx: Redirection (further action must be taken to complete request)
4 4xx: Client Error (request contains bad syntax and can't be completed)
5 5xx: Server Error (server couldn't complete request)

You've probably seen a bunch of these before, the most common being 404. This means that the server couldn't find the route you requested.

Status Codes in Rack

In Rack, we are able to set the response's status code by just setting the status_code attribute. By default, Rack sets a status code of 200. But when a user selects a route that doesn't exist, we need to set the status to 404.

class Application
  
  def call(env)
    resp = Rack::Response.new
    req = Rack::Request.new(env)

    if req.path=="/songs"
      resp.write "You requested the songs"
    else
      resp.write "Route not found"
      resp.status = 404
    end

    resp.finish
  end
end

Now if you go to localhost:9292/badURL you'll get the error message, and if you open up the Inspect Element navigator you'll see something like this:

Video Reviews

Resources

View HTTP Status Codes on Learn.co and start learning to code for free.

rack-status-codes-readme's People

Contributors

annjohn avatar deniznida avatar fislabstest avatar fs-lms-test-bot avatar irmiller22 avatar sarogers avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.