405 HTTP Code: A Comprehensive Guide to the 405 Method Not Allowed

The 405 HTTP code, widely known as “Method Not Allowed,” is a standard response in the HTTP protocol. When a client requests a resource using an HTTP method that the server recognises but is not allowed for that particular resource, the server returns this status code. In practice, developers encounter the 405 http code during API design, web application development, content delivery, and when debugging misconfigured servers. This guide explores what the 405 HTTP code means, why it appears, how to react to it, and how to prevent it from becoming a barrier to user experience or search performance.
What is the 405 HTTP code and why does it appear?
The 405 HTTP code is part of the 4xx class of client error responses. It indicates that the HTTP method used in the request is known by the server but not allowed for the requested resource. For example, attempting to POST to a resource that only supports GET will provoke a 405 response. The server’s message typically includes an Allow header listing the methods that are permitted for the resource. This mechanism helps clients discover which operations are valid, reducing blind attempts and potential misuse of endpoints.
HTTP 405: common patterns and scenarios
RESTful APIs and resource methods
In RESTful designs, resources are manipulated using a limited set of methods: GET, POST, PUT, PATCH, DELETE, OPTIONS, and HEAD. If a client tries to delete a resource that is read-only, or update a resource that should only be retrieved, the server should respond with the 405 HTTP code and an Allow header. This communicates clearly which operations are permissible and preserves the intended semantics of the API.
Web forms and static content
Even outside APIs, the 405 http code arises when clients use methods beyond what a server-side resource accepts. For instance, a static HTML file might be served with only GET allowed; if a client attempts PUT or DELETE, the server can return a 405 to reflect that the action is not supported on that resource.
Crawlers, bots and automation
Automated clients sometimes probe an endpoint with a variety of methods to test capabilities. A properly configured server returns the 405 HTTP code in response to unsupported methods, guiding the bot to use the OPTIONS method or to consult the API documentation for allowed operations.
Technical background: how 405 fits into HTTP semantics
Location in the status code taxonomy
The 405 code sits in the 4xx family, which denotes client-side issues. Specifically, 405 is distinct from 404 (Not Found) and 403 (Forbidden) as it communicates a mismatch between the method used and the server’s rules for the resource. The response should include an Allow header listing the permitted methods and, when possible, a human-readable explanation of why the method is not allowed.
Differences between 405 and 406
While a 406 (Not Acceptable) can indicate that the server cannot produce a response matching the Accept headers sent by the client, the 405 HTTP code indicates a method-level restriction. In practice, a 406 is about content negotiation, whereas a 405 is about the action being attempted on the resource itself.
Best practices for handling the 405 HTTP code
Providing helpful Allow headers
When returning a 405 http code, servers should include an Allow header that enumerates the accepted methods for the requested resource. This helps clients quickly adapt and retry with an allowed method. For example, a response might include:
HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD
Where feasible, the server should also present a human-friendly explanation in the response body, guiding developers and end users toward the correct method and usage.
Clear error messages and documentation links
Descriptive messages in the response body can reduce frustration for developers integrating with an API. A concise message such as “Method POST is not allowed for /resource. Use GET or HEAD instead.” paired with a link to the API documentation enhances usability and reduces ticket volume.
Idempotence and safety considerations
Be mindful of the implications of the methods you allow. For public APIs, ensuring that safe methods (like GET) can be repeated without side effects is important. When you respond with the 405 http code, consider whether the allowed methods align with the API’s safety and idempotence guarantees.
Handling 405 in server configurations and codebases
Apache
In Apache, a 405 response can occur due to Limit or LimitExcept directives restricting methods. If you encounter 405 errors, verify the AllowOverride, DirectoryIndex, and Require directives. An explicit requirement is to ensure the server provides the correct Allow header via the configured handler or module.
Nginx
With Nginx, you may need to ensure that your location blocks permit the methods you intend and that any upstream services return the appropriate headers. A common approach is to configure the limit_except block to control which methods are allowed for a given location. If a method is not allowed, Nginx will respond with 405 and an Allow header listing the accepted methods, provided the configuration supports it.
Express (Node.js)
In Express, you can define route handlers for specific methods and rely on the framework to send a 405 if another method is used. If you want to enforce a strict policy, you can add middleware that sends a 405 with an explicit Allow header when an unsupported method is detected. This improves clarity for API consumers.
Django
Django’s URL dispatching enables or denies HTTP methods through view logic or via decorators, such as @require_http_methods. When a request uses a disallowed method, Django can return a 405 with a standard response. Developers should ensure that the response includes the proper status code and a helpful message when appropriate.
Laravel and other frameworks
Many PHP frameworks, including Laravel, offer route-level restrictions on methods. Configuring routes to only respond to permitted methods helps ensure that the server returns the correct 405 HTTP code and the accompanying Allow header for any disallowed requests.
Client-side strategies: how to cope with 405 http code
Detecting and retrying with allowed methods
Clients should be prepared to handle a 405 by consulting the response’s Allow header. If the client is a browser, users may simply refresh with an appropriate method or navigate to the correct resource. For programmatic clients, implement logic to parse the Allow header and adjust the request method accordingly.
Rate limiting and user experience
If an endpoint consistently returns 405 for a subset of requests, you may be dealing with a design or documentation gap. Providing clear API documentation and an example codelist of allowed methods reduces confusion. Rate limiting should be applied in a way that does not convert legitimate method variations into 405 errors due to misconfiguration.
How 405 http code interacts with caching and search engines
Browser caching and 405
Browsers generally do not cache 405 responses aggressively, since they are often tied to authorisation or resource state that may change. However, intermediate caches and reverse proxies can store 405 responses. It’s important to configure caches to refresh when resource permissions change or when actual method allowances are updated.
SEO and indexing considerations
From an SEO perspective, a 405 response can affect how search engines interpret endpoints. If a resource should be accessible with GET but uses a 405 to signal disallowed methods, ensure that the primary content remains reachable via GET and that canonical URLs are correctly defined. Documentation endpoints should be predictable and stable, returning 200 for allowed retrieval and 405 for disallowed methods to maintain semantics.
Common confusion: 405 vs other 4xx errors
405 versus 401 and 403
401 indicates authentication is required or has failed, while 403 means the client is authenticated but not authorised to access the resource. The 405 http code, by contrast, means the requested method is not allowed for the resource, regardless of authentication. Distinguishing these codes helps both developers and security professionals diagnose issues accurately.
405 versus 404
A 404 Not Found means the resource does not exist at the given URL, while a 405 shows that the resource exists but cannot support the attempted operation. In well-designed APIs, you should see 404 for missing endpoints and 405 for unsupported methods on existing endpoints.
Historical context and evolution of the 405 HTTP code
The 405 Method Not Allowed status has its roots in the early days of the HTTP specification as clients and servers began negotiating capability and semantics over the web. As APIs proliferated and security considerations grew, the 405 code became a standard tool for indicating method-level restrictions without altering the resource’s identity. Today, it remains a clear signal to clients that an operation is disallowed for the resource in question, encouraging proper usage and reducing stray requests that could cause unintended side effects.
Real-world examples and practical tips
Example: a read-only resource
Suppose a resource at /api/static-content is intended for reading only. If a client sends a POST request to this endpoint, the server should respond with a 405 HTTP code and an Allow header listing GET and HEAD as permitted methods. A concise body message could read: “This resource supports GET and HEAD only.”
Example: updating an item via an API
For an endpoint like /api/items/{id}, if the server only supports GET and DELETE, but a client sends PUT, the response would be 405 Method Not Allowed with Allow: GET, DELETE. This approach communicates precisely which operations are supported for the resource.
Example: using OPTIONS for discovery
The OPTIONS method can be used by clients to discover allowed methods for a resource. A well-implemented API may return a 200 with an Allow header in response to an OPTIONS request, even if the actual resource would otherwise return 405 for disallowed methods. This helps clients adapt without trial and error.
Design considerations: building resilient systems around 405 http code
Universality of method semantics
Ensure that the method semantics you implement are consistent across the entire API or website. Inconsistent handling of methods across endpoints leads to confusion and user frustration, increasing the likelihood of 405 errors that aren’t meaningful.
Documentation and developer experience
Maintain comprehensive documentation that lists what methods are allowed for each resource. Include examples of typical requests, expected responses, and the structure of the Allow header. The better your documentation, the fewer 405 errors you will see in production.
Security implications
Avoid exposing sensitive information via error bodies when a 405 occurs. Keep error messages concise and avoid revealing internal URIs or server configuration details. The primary goal is clarity for legitimate clients while not increasing attack surface.
Conclusion: mastering the 405 http code for robust web services
The 405 HTTP code is a purposeful signal in the HTTP ecosystem. It tells clients that their chosen method is recognised but not permitted for the targeted resource. By returning an appropriate Allow header, offering helpful messages, and keeping endpoints consistent, developers can create smoother integrations and improve the reliability of their services. Remember to align server configurations, API design, and client expectations so that 405 responses are informative rather than frustrating. In practice, a well-implemented 405 http code becomes a tool for clarity, not a barrier to functionality.
Further reading and next steps
To deepen your understanding of the 405 http code, consider auditing your API endpoints with a method matrix that maps each resource to its allowed HTTP methods. Integrate automated tests that verify not only functional success cases but also that disallowed method requests return 405 with correct headers. Review caching rules in front of these endpoints to ensure that 405 responses don’t linger longer than intended. By approaching the 405 http code with a structured, reader-friendly mindset, you can improve both developer experience and application resilience.