Last updated
Last updated
Define status codes and what they communicate to a client
Describe the structure and various categories of status codes
Set a response's status code in Rack
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.
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.
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
.
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:
View on Learn.co and start learning to code for free.
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)