Skip to content

Return 405 Method Not Allowed when a path exists but the HTTP method is unsupported #4633

@silvioprog

Description

@silvioprog

What is the feature you are proposing?

Add a built-in, global handler such as app.methodNotAllowed() that is triggered when a path matches an existing route but the HTTP method is not supported. This handler would return 405 Method Not Allowed and optionally include an Allow header listing the permitted methods.

This should be an opt-in feature, so Hono keeps its current behavior by default, but allows projects that want stricter HTTP semantics to enable proper 405 handling.

Problem

Currently, when a request is made to a path that exists but the HTTP method is not supported, Hono returns 404 Not Found. From an HTTP semantics perspective, this is misleading: the resource exists, but the method is not allowed. The correct response in this case should be 405 Method Not Allowed.

This behavior makes it harder for API clients and developers to distinguish between:

  • A route that truly does not exist (404)
  • A route that exists but does not support the given method (405)

Also, the preferred behavior can vary by project. Some APIs want strict HTTP compliance and expect 405, while others prefer returning 404 for security reasons. Hono should not enforce one approach globally.

Expected Behavior

When app.methodNotAllowed() is configured and a path is registered but the requested HTTP method is not supported, Hono should:

  1. Return 405 Method Not Allowed
  2. Include an Allow header listing the supported methods for that path

This follows the HTTP specification and matches the behavior of many other web frameworks.

Example

app.get('/hello', (c) => c.text('Hello'))

A POST /hello request should return:

405 Method Not Allowed
Allow: GET

instead of:

404 Not Found

Benefits

  • Keeps Hono’s current secure default behavior
  • Allows projects to opt into proper HTTP semantics
  • Better alignment with HTTP standards
  • Clearer API semantics for consumers
  • Easier debugging and client-side error handling
  • More predictable behavior for developers building RESTful APIs with Hono

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions