Generate random HTTP status codes for testing web applications, API responses, and error handling. Learn about different status code categories and their meanings. Perfect for web developers, QA engineers, and API testers.
HTTP status codes are three-digit numbers returned by a server in response to a client's request made to the server. These codes are grouped into five classes, each serving a specific purpose in HTTP communication.
Category | Range | Description | Examples |
---|---|---|---|
Informational | 100-199 | The request was received, continuing process | 100 Continue, 101 Switching Protocols |
Success | 200-299 | The request was successfully received, understood, and accepted | 200 OK, 201 Created, 204 No Content |
Redirection | 300-399 | Further action needs to be taken to complete the request | 301 Moved Permanently, 302 Found, 304 Not Modified |
Client Error | 400-499 | The request contains bad syntax or cannot be fulfilled | 400 Bad Request, 401 Unauthorized, 404 Not Found |
Server Error | 500-599 | The server failed to fulfill a valid request | 500 Internal Server Error, 503 Service Unavailable |
When developing web applications or APIs, it's crucial to test how your code handles different HTTP status codes. Here are some common testing scenarios:
// Node.js Express Example
const express = require('express');
const app = express();
// Success response (200 OK)
app.get('/api/success', (req, res) => {
res.status(200).json({ message: 'Operation successful' });
});
// Created resource (201 Created)
app.post('/api/resources', (req, res) => {
// Create resource logic here
res.status(201).json({ id: 123, message: 'Resource created' });
});
// Not Found error (404)
app.get('/api/missing', (req, res) => {
res.status(404).json({ error: 'Resource not found' });
});
// Unauthorized error (401)
app.get('/api/protected', (req, res) => {
if (!req.headers.authorization) {
return res.status(401).json({ error: 'Authentication required' });
}
// Process protected resource
});
// Server error (500)
app.get('/api/error', (req, res) => {
try {
// Some operation that might fail
throw new Error('Something went wrong');
} catch (err) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});