The 405 Method Not Allowed HTTP status code indicates that the server understands the request, but the HTTP method used (GET, POST, PUT, DELETE, etc.) is not permitted for the requested resource.
Allow header with permitted methodsWhen a client sends a request using an unsupported HTTP method, the server responds with status code 405 instead of processing the request.
// Example of an HTTP 405 response header
HTTP/1.1 405 Method Not Allowed
Allow: GET, POST
Content-Type: text/html
// Express.js route allowing only GET requests
app.get("/users", (req, res) => {
res.send("Users list");
});
If a client sends a POST request to /users, the server will reject it because only GET is defined.
// HTML form incorrectly using POST on a GET-only endpoint
<form method="POST" action="/users">
<button>Submit</button>
</form>
Target Endpoint: /api/public-news
Allowed Methods: GET only
Allow header for better debugging