HTTP Status Codes: An Overview

rails,

Swapnil Gourshete Swapnil Gourshete Follow Jul 25, 2024 · 8 mins read
HTTP Status Codes: An Overview


HTTP status codes are an essential part of web communication, indicating the result of a client’s request to a server. Here’s an expanded list of status codes, their meanings, and how to use them in a Rails API.


1xx: Informational

100

code - 100
status - :continue
Info - Continue

# This is typically handled by the server automatically

101

code - 101
status - :switching_protocols
Info - Switching Protocols

# This is typically handled by the server automatically

102

code - 102
status - :processing
Info - Processing

# This is typically used in WebDAV

2xx: Success

200

code - 200
status - :ok
Info - OK

render json: { message: 'Success' }, status: :ok

201

code - 201
status - :created
Info - Created

render json: { message: 'Resource created' }, status: :created

202

code - 202
status - :accepted
Info - Accepted

render json: { message: 'Request accepted for processing' }, status: :accepted

203

code - 203
status - :non_authoritative_information
Info - Non-Authoritative Information

render json: { message: 'Information from a third-party source' }, status: :non_authoritative_information

204

code - 204
status - :no_content
Info - No Content

head :no_content

205

code - 205
status - :reset_content
Info - Reset Content

head :reset_content

206

code - 206
status - :partial_content
Info - Partial Content

render json: { partial_data: data }, status: :partial_content

3xx: Redirection

300

code - 300
status - :multiple_choices
Info - Multiple Choices

render json: { choices: options }, status: :multiple_choices

301

code - 301
status - :moved_permanently
Info - Moved Permanently

redirect_to new_url, status: :moved_permanently

302

code - 302
status - :found
Info - Found

redirect_to other_url, status: :found

303

code - 303
status - :see_other
Info - See Other

redirect_to other_url, status: :see_other

304

code - 304
status - :not_modified
Info - Not Modified

head :not_modified

307

code - 307
status - :temporary_redirect
Info - Temporary Redirect

redirect_to temp_url, status: :temporary_redirect

308

code - 308
status - :permanent_redirect
Info - Permanent Redirect

redirect_to new_permanent_url, status: :permanent_redirect

4xx: Client Errors

400

code - 400
status - :bad_request
Info - Bad Request

render json: { error: 'Invalid parameters' }, status: :bad_request

401

code - 401
status - :unauthorized
Info - Unauthorized

render json: { error: 'Authentication required' }, status: :unauthorized

402

code - 402
status - :payment_required
Info - Payment Required

render json: { error: 'Payment required to access this resource' }, status: :payment_required

403

code - 403
status - :forbidden
Info - Forbidden

render json: { error: 'Access denied' }, status: :forbidden

404

code - 404
status - :not_found
Info - Not Found

render json: { error: 'Resource not found' }, status: :not_found

405

code - 405
status - :method_not_allowed
Info - Method Not Allowed

render json: { error: 'Method not allowed' }, status: :method_not_allowed

406

code - 406
status - :not_acceptable
Info - Not Acceptable

render json: { error: 'Not acceptable' }, status: :not_acceptable

409

code - 409
status - :conflict
Info - Conflict

render json: { error: 'Resource conflict' }, status: :conflict

410

code - 410
status - :gone
Info - Gone

render json: { error: 'Resource no longer available' }, status: :gone

422

code - 422
status - :unprocessable_entity
Info - Unprocessable Entity

render json: { errors: resource.errors }, status: :unprocessable_entity

429

code - 429
status - :too_many_requests
Info - Too Many Requests

render json: { error: 'Rate limit exceeded' }, status: :too_many_requests

5xx: Server Errors

500

code - 500
status - :internal_server_error
Info - Internal Server Error

render json: { error: 'Internal Server Error' }, status: :internal_server_error

501

code - 501
status - :not_implemented
Info - Not Implemented

render json: { error: 'Functionality not implemented' }, status: :not_implemented

502

code - 502
status - :bad_gateway
Info - Bad Gateway

render json: { error: 'Bad Gateway' }, status: :bad_gateway

503

code - 503
status - :service_unavailable
Info - Service Unavailable

render json: { error: 'Service Unavailable' }, status: :service_unavailable

504

code - 504
status - :gateway_timeout
Info - Gateway Timeout

render json: { error: 'Gateway Timeout' }, status: :gateway_timeout

507

code - 507
status - :insufficient_storage
Info - Insufficient Storage

render json: { error: 'Insufficient storage' }, status: :insufficient_storage

511

code - 511
status - :network_authentication_required
Info - Network Authentication Required

render json: { error: 'Network authentication required' }, status: :network_authentication_required

Note that while Rails provides symbols for many status codes, you can always use the numerical code directly if a symbol is not available:

render json: { message: 'Custom status' }, status: 418

When building APIs, using appropriate status codes helps clients understand and handle responses correctly, leading to more robust and maintainable applications. It’s important to choose the most specific and appropriate status code for each situation to provide clear and accurate information to API consumers.


Swapnil Gourshete
Written by Swapnil Gourshete Follow
Hi I am Swapnil, a Software Engineer and computer science enthusiastic