Interview

HTTP response status codes

Aug 22, 2024, 6:08 PM
Blog Thumbnail

Complete HTTP Status Codes Guide

What is HTTP Codes

HTTP state's codes attempts to give a standard response to every request received by the web server from a client. Such codes indicate whether the request was fulfilled, redirected, generated an error, or needed further actions, and such codes also can be distributed to five classes depending on the response rendered.

http error code banner

Every HTTP status code you need to know

ℹ️ 1xx: Informational Responses

error code 100 "Continue"

server received request headers and continue with the request.

HTTP/1.1 100 Continue
    

101 Switching Protocols

Server switching protocols according to requested by client (e.g., upgrading to WebSocket ).

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
    

102 Processing

server received request and starting process but not available response

HTTP/1.1 102 Processing
    

103 Early Hints

This status code is primarily intended to be used with the Link header, letting the user agent start preloading resources while the server prepares a response or preconnect to an origin from which the page will need resources.

✓ 2xx: Success Responses

200 OK

Request succeeded. The default success status.

HTTP/1.1 200 OK
Content-Type: application/json

{
    "status": "success",
    "data": { "message": "Request successful run and return output" }
}
    

201 Created

Request succeeded and a new resource was created.

HTTP/1.1 201 Created
Location: /api/users/udaipur
    

202 Accepted

Request accepted by server but is pending stage not complete.

HTTP/1.1 202 Accepted
Content-Type: application/json

{
    "status": "processing",
    "estimated-Completion-Time": "2024-10-30"
}
    

204 No Content

client send request to server but not return any content.

HTTP/1.1 204 No Content
    

↪ 3xx: Redirection Messages

301 Moved Permanently

Requested url redirect to another url permanent.

HTTP/1.1 301 Moved Permanently
Location: https://newdomain.com/video
    

302 Found (Temporary Redirect url )

Requested url redirect to another url temporarily.

HTTP/1.1 302 Found
Location: https://example.com/temporary-page-found
    

304 Not Modified

Resource hasn't changed since the last request.

HTTP/1.1 304 Not Modified
ETag: "81648761246715764681264871724681"
    

! 4xx: Client Error Responses

400 Bad Request

when client side error server not process request.

HTTP/1.1 400 Bad Request
Content-Type: application/json

{
    "error": "Invalid parameters",
    "details": {
        "email": "Invalid email format please enter valid email "
    }
}
    

401 Unauthorized

When user request without token and actual token to authenticate website when show error.

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="example-token"
    

402 Payment Required

when payment is required for access some website features.

HTTP/1.1 402 Payment Required
Content-Type: application/json

{
    "error": "Subscription required",
    "plans": ["basic", "premium", "silver"]
}
    

403 Forbidden

Server understands request but refuses to authorize it.

HTTP/1.1 403 Forbidden
Content-Type: application/json

{
    "error": "Access denied",
    "message": "Insufficient permissions"
}
    

404 Not Found

The requested resource could not be found.

HTTP/1.1 404 Not Found
Content-Type: application/json

{
    "error": "Resource not found",
    "message": "The requested URL was not found"
}
    
http error code 404

405 Method Not Allowed

HTTP method not allowed for this endpoint.

HTTP/1.1 405 Method Not Allowed
Allow: GET, POST
    

429 Too Many Requests

User has sent too many requests in a given time.

HTTP/1.1 429 Too Many Requests
Retry-After: 60
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 0
    

⚠ 5xx: Server Error Responses

500 Internal Server Error

A generic error message when an unexpected condition was encountered.

http error code 500
HTTP/1.1 500 Internal Server Error
Content-Type: application/json

{
    "error": "Internal Server Error",
    "message": "An unexpected error occurred",
    "requestId": "abc-123-xyz"
}
    

Common causes: - Unhandled exceptions - Database connection issues - Configuration errors - Memory issues

501 Not Implemented

the server not support request functionality to fullfill request requirement

HTTP/1.1 501 Not Implemented
Content-Type: application/json

{
    "error": "Not Implemented ",
    "message": "Thi api end point is not available please check endpoint ",
    "availableDate": "2024-12-30"
}
    

502 Bad Gateway

when server received invalid server response from parent/upstrean server.

HTTP/1.1 502 Bad Gateway
Content-Type: application/json

{
    "error": "Bad Gateway",
    "message": "Invalid response from upstream server",
    "retryAfter": 30
}
    

Common scenarios: - Microservice failures - Invalid proxy configurations - Network issues between servers

503 Service Unavailable

when server not handle request because already use 100% for other request when error generate.

HTTP/1.1 503 Service Unavailable
Retry-After: 300
Content-Type: application/json

{
    "error": "Service Unavailable",
    "message": "System is under maintenance",
    "estimatedResolution": "2024-10-27T 15:30:00Z"
}
    

504 Gateway Timeout

the server acting as gateway but not received response from parent/upstream server

HTTP/1.1 504 Gateway Timeout
Content-Type: application/json

{
    "error": "Gateway Timeout again try",
    "message": "Upstream server failed to respond in time",
    "timeout": 30
}
    

505 HTTP Version Not Supported

the server is not support this version used in request please upgrade version

HTTP/1.1 505 HTTP Version Not Supported
Content-Type: application/json

{
    "error": "HTTP Version Not Supported",
    "message": "This server only supports HTTP/1.1 and HTTP/2",
    "supportedVersions": ["HTTP/1.1", "HTTP/2"]
}
    

506 Variant Also Negotiates

The server has an internal configuration error: the chosen variant resource is configured to engage in transparent content negotiation itself.

HTTP/1.1 506 Variant Also Negotiates
Content-Type: application/json

{
    "error": "Variant Also Negotiates",
    "message": "Server configuration error in content negotiation"
}
    

507 Insufficient Storage

when server memory is full and not handle and store more request data.

HTTP/1.1 507 Insufficient Storage
Content-Type: application/json

{
    "error": "Insufficient Storage",
    "message": "Not enough storage space to complete the request",
    "availableSpace": "100MB",
    "requiredSpace": "500MB"
}
    

508 Loop Detected

The server detected an infinite loop while processing the request.

HTTP/1.1 508 Loop Detected
Content-Type: application/json

{
    "error": "Loop Detected",
    "message": "Infinite redirect loop detected",
    "path": "/a -> /b -> /c -> /a"
}
    

510 Not Extended

Further extensions to the request are required for the server to fulfill it .

HTTP/1.1 510 Not Extended
Content-Type: application/json

{
    "error": "Not Extended ",
    "message": "requires protocol extension ",
    "requiredExtensions": ["livedemo-ext"]
}
    

511 Network Authentication Required

The client needs to authenticate to gain network access.

HTTP/1.1 511 Network Authentication Required
Content-Type: application/json

{
    "error": "Authentication Required for access ",
    "message": "first  authenticate with user_id password to access the network",
    "loginUrl": "https://wifi.newwifisetup.com/login"
}
    

Handle 5xx Errors

  • Always include a unique request ID for tracking
  • Provide clear, user-friendly error messages
  • Implement proper logging and monitoring
  • Add retry mechanisms for temporary failures
  • Use appropriate status codes for specific scenarios
  • Include helpful debugging information in development

Keep your servers healthy and your error handling robust! 🚀