← Back to Chapters

405 Method Not Allowed

? 405 Method Not Allowed

? Quick Overview

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.

? Key Concepts

  • The resource exists on the server
  • The request method is recognized
  • The method is blocked for this endpoint
  • The server usually sends an Allow header with permitted methods

? Syntax / Theory

When a client sends a request using an unsupported HTTP method, the server responds with status code 405 instead of processing the request.

? View Code Example
// Example of an HTTP 405 response header
HTTP/1.1 405 Method Not Allowed
Allow: GET, POST
Content-Type: text/html

? Code Example(s)

? View Code Example
// Express.js route allowing only GET requests
app.get("/users", (req, res) => {
res.send("Users list");
});

? Live Output / Explanation

What Happens?

If a client sends a POST request to /users, the server will reject it because only GET is defined.

? Interactive Example

? View Code Example
// HTML form incorrectly using POST on a GET-only endpoint
<form method="POST" action="/users">
<button>Submit</button>
</form>

? API Simulator

Target Endpoint: /api/public-news
Allowed Methods: GET only

// Click a button to simulate a request...

? Use Cases

  • REST APIs with strict HTTP method rules
  • Security hardening for endpoints
  • Preventing unintended data modification
  • Enforcing proper API usage

? Tips & Best Practices

  • Always define allowed HTTP methods clearly
  • Return an Allow header for better debugging
  • Use REST conventions consistently
  • Log 405 errors for API misuse detection

? Try It Yourself

  • Create an endpoint that only allows GET
  • Send POST and observe the 405 error
  • Check the response headers
  • Fix the method and retry