DEV Community

Davide Santangelo
Davide Santangelo

Posted on

HTTP Status Codes: An In-Depth Explanation with Ruby Examples

HTTP (Hypertext Transfer Protocol) is the foundation of data communication on the World Wide Web. When a client (such as a web browser) sends a request to a web server, the server responds with an HTTP status code to inform the client about the outcome of the request. These status codes are three-digit numbers that provide valuable information about what happened during the request-response cycle. In this article, we will explain the most common HTTP status codes in a comprehensible manner and provide examples using Ruby programming language.

Informational (1xx)

100 - Continue: The server received the initial part of the request, and the client should continue with the remainder.

require 'net/http'
uri = URI('http://example.com')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)

if response.is_a? Net::HTTPContinue
    puts "Continue: Server is ready for more data"
end
Enter fullscreen mode Exit fullscreen mode

Successful (2xx)

200 - OK: The request was successful, and the server sends back the requested data.

require 'net/http'
uri = URI('http://example.com')
response = Net::HTTP.get_response(uri)

if response.is_a? Net::HTTPOK
    puts "OK: Request was successful"
end
Enter fullscreen mode Exit fullscreen mode

201 - Created: The request was successful, and a new resource was created on the server.

require 'net/http'
require 'json'

uri = URI('http://example.com/resource')
data = { 'name' => 'New Resource' }

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
request.body = data.to_json

response = http.request(request)

if response.is_a? Net::HTTPCreated
    puts "Created: Resource successfully created"
end
Enter fullscreen mode Exit fullscreen mode

Redirection (3xx)

301 - Moved Permanently: The requested resource has been permanently moved to a new URL.

require 'net/http'

uri = URI('http://example.com/old-location')
response = Net::HTTP.get_response(uri)

if response.is_a? Net::HTTPMovedPermanently
    puts "Moved Permanently: Resource has moved to a new location"
end
Enter fullscreen mode Exit fullscreen mode

302 - Found (or 303 - See Other): The resource has temporarily moved to a new URL.

require 'net/http'

uri = URI('http://example.com/temp-location')
response = Net::HTTP.get_response(uri)

if response.is_a?(Net::HTTPFound) || response.is_a?(Net::HTTPSeeOther)
    puts "Found/See Other: Resource temporarily moved"
end
Enter fullscreen mode Exit fullscreen mode

Client Error (4xx)

400 - Bad Request: The server cannot understand the client's request due to invalid syntax or other errors.

require 'net/http'

uri = URI('http://example.com/invalid-resource')
response = Net::HTTP.get_response(uri)

if response.is_a? Net::HTTPBadRequest
    puts "Bad Request: Client's request is invalid"
end
Enter fullscreen mode Exit fullscreen mode

404 - Not Found: The requested resource could not be found on the server.

require 'net/http'

uri = URI('http://example.com/nonexistent-resource')
response = Net::HTTP.get_response(uri)

if response.is_a? Net::HTTPNotFound
    puts "Not Found: Resource does not exist"
end
Enter fullscreen mode Exit fullscreen mode

Server Error (5xx)

500 - Internal Server Error: The server encountered an error while processing the request.

require 'net/http'

uri = URI('http://example.com/error-resource')
response = Net::HTTP.get_response(uri)

if response.is_a? Net::HTTPInternalServerError
    puts "Internal Server Error: Server encountered an error"
end
Enter fullscreen mode Exit fullscreen mode

503 - Service Unavailable: The server is temporarily unable to handle the request.

require 'net/http'

uri = URI('http://example.com/unavailable-service')
response = Net::HTTP.get_response(uri)

if response.is_a? Net::HTTPServiceUnavailable
    puts "Service Unavailable: Server cannot handle the request"
end
Enter fullscreen mode Exit fullscreen mode

Here is a detailed table of all the HTTP status codes along with their descriptions:

Status Code Description
1xx - Informational
100 Continue - The server has received the initial request and the client should proceed with the rest of the request.
101 Switching Protocols - The server is changing protocols per the client's request.
102 Processing - The server is processing the request but has not completed it yet.
2xx - Successful
200 OK - The request has been successfully fulfilled, and the server sends back the requested data.
201 Created - The request has been successfully fulfilled, and a new resource has been created on the server.
202 Accepted - The request has been accepted, but processing is not yet completed.
204 No Content - The request was successful, but there is no additional information to send back.
206 Partial Content - The server is delivering only part of the resource due to a range request.
3xx - Redirection
300 Multiple Choices - The request has multiple possible responses, and the user should select one.
301 Moved Permanently - The requested resource has been permanently moved to a new URL.
302 Found - The resource has temporarily moved to a new URL.
303 See Other - The resource can be found at a different URL, and the client should retrieve it using a GET method.
304 Not Modified - The resource has not been modified since the specified date.
307 Temporary Redirect - The request should be repeated with another URI, but future requests should still use the original URI.
308 Permanent Redirect - The request and all future requests should be repeated using another URI.
4xx - Client Errors
400 Bad Request - The server cannot understand the client's request, typically due to invalid syntax or other errors.
401 Unauthorized - The request requires user authentication, and the client must provide valid credentials.
403 Forbidden - The server refuses to fulfill the request, and the client does not have permission to access the resource.
404 Not Found - The requested resource could not be found on the server.
405 Method Not Allowed - The HTTP method in the request is not allowed for the specified resource.
406 Not Acceptable - The requested resource cannot generate content that is acceptable according to the headers sent in the request.
408 Request Timeout - The server timed out while waiting for the request.
409 Conflict - The request could not be completed due to a conflict with the current state of the target resource.
410 Gone - The requested resource is no longer available, and the server has no forwarding address.
413 Payload Too Large - The server refuses to process a request because the request payload is too large.
429 Too Many Requests - The user has sent too many requests in a given amount of time.
5xx - Server Errors
500 Internal Server Error - The server encountered an error while processing the request.
501 Not Implemented - The server does not support the functionality required to fulfill the request.
502 Bad Gateway - The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed.
503 Service Unavailable - The server is temporarily unable to handle the request, typically due to overloading or maintenance.
504 Gateway Timeout - The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server or application.
505 HTTP Version Not Supported - The server does not support the HTTP protocol version used in the request.

HTTP status codes are essential for understanding the outcome of HTTP requests and responses. They help both clients and servers communicate effectively and handle various situations. By using these codes along with your Ruby applications, you can develop more robust and user-friendly web services.

Top comments (0)