REST API - why use PUT DELETE POST GET?
You might have encountered a common debate when designing web services: should every endpoint handle its logic based on the HTTP verb, or is it easier to just send a JSON payload containing an action name via a standard POST or GET request?
Some developers argue that splitting your routing logic like this makes sense:
$method = $_SERVER['REQUEST_METHOD'];
$request = explode("/", substr($_SERVER['PATH_INFO'], 1)); // Corrected syntax from source
switch ($method) {
case 'PUT':
// Handle update logic
break;
case 'POST':
// Handle creation logic
break;
case 'GET':
// Handle retrieval logic
break;
case 'DELETE':
// Handle removal logic
break;
}
This approach treats the HTTP method as a generic command tag, similar to how XML-RPC or JSON-RPC protocols operate. You send a single path (e.g., /users/1) and rely entirely on the request body or headers to dictate what happens. While this reduces the need for complex routing tables in your framework configuration, it fundamentally ignores the semantic meaning of HTTP verbs defined by RFC 7231.
If you choose this "method-in-payload" approach, you are effectively building a RESTful-like API over RPC (Remote Procedure Call). This works fine for internal tools or simple scripts where strict adherence to web standards isn't a priority. However, when aiming for a scalable public API, relying on a single POST method for everything introduces significant friction.
The Problem with Ambiguity
The primary issue with sending an action name inside the JSON body is that it conflates the resource with the operation. Consider the endpoint /users/1.
If you send a GET request to this URL, the expectation is clear: retrieve the user data.
If you send a DELETE request, the expectation is clear: remove the user.
When you force both operations into a POST request with a body like {"action": "delete"}, you lose two critical pieces of information immediately visible to the client and intermediaries. First, caching layers (like Varnish or CDN) cannot easily determine if they should serve a cached response for a delete operation because there is no standard cache-control associated with an arbitrary JSON payload. Second, browser developers tools and network monitors become cluttered with opaque POST requests that don't immediately reveal their intent just by looking at the URL.
Semantic HTTP Methods
Using specific verbs like PUT, DELETE, POST, and GET isn't just about convention; it defines the nature of the operation itself.
When you use PUT /users/1, you are stating that the resource at /users/1 should be replaced entirely with the data provided in the body. This is an idempotent operation, meaning calling it ten times has the same result as calling it once. It also suggests that the client controls the representation of the resource.
Conversely, POST implies creation or a side effect that doesn't necessarily map to an existing resource state in the same way. If you send POST /users/1 with a body containing user details, you are creating a new resource. If you use the same URL for deletion via POST, you break the semantic contract.
Why Frameworks Prefer Verbs
Modern frameworks often simplify this by allowing you to define routes that map directly to these verbs. In Laravel, for example, you don't write a giant switch statement; you define distinct routes:
// Define specific routes for specific HTTP methods
Route::get('/users', [UserController::class, 'index']);
Route::post('/users', [UserController::class, 'store']);
Route::put('/users/{id}', [UserController::class, 'update']);
Route::delete('/users/{id}', [UserController::class, 'destroy']);
This structure allows the framework's router to match the incoming request method directly to a controller method. It makes your codebase more readable and aligns with how clients interact with REST APIs. Tools like Postman or Swagger/OpenAPI generators rely heavily on these standard methods to document your API effectively. If you only expose POST for everything, generating documentation becomes an exercise in reverse-engineering your JSON payloads rather than defining a clear contract.
When is the "One Method" Approach Viable?
There are scenarios where sticking to a single POST method is acceptable. Some large-scale platforms, like Stack Exchange, have historically used this approach for their public APIs. The rationale is often simplicity of implementation and the ability to evolve endpoints without breaking existing clients that might be hard-coded to use POST.
However, if you are building a new API or migrating an existing system, adopting standard HTTP verbs offers better long-term maintainability. It leverages the built-in capabilities of HTTP proxies, browsers, and caching strategies without requiring custom logic to interpret every request body.
Ultimately, while using $_SERVER['REQUEST_METHOD'] with a switch statement is technically functional, it trades semantic clarity for implementation convenience. Unless you have specific constraints forcing a single-method design, utilizing the full spectrum of HTTP verbs provides a more robust foundation for your API architecture.