skip to content

In a web framework, what is returned by default when a request's path matches a route but its method does not?

level: middleimportance: should knowfreq 50%

answer

  1. path known, method not
  2. depends how the router indexes routes
  3. 405 when the path stays visible
  4. 404 when method joins the lookup key
  5. auto-handled OPTIONS and HEAD skew it

basics

~20 s

Most frameworks answer 405 Method Not Allowed, because the router can see the path is registered under other methods. Some answer 404 instead, either because method and path form one lookup key or to avoid confirming that the path exists.

solid answer

~50 s

It depends on how the router indexes routes. A router that matches the path first and the method second can tell that the path exists but not for this method, so its built-in fallback answers `405 Method Not Allowed`, usually filling the `Allow` header from the methods registered for that path. A router that treats method and path as one composite key sees only a miss, and the request takes the same branch as an unknown path, producing `404`. Some frameworks choose `404` deliberately so a prober cannot enumerate which paths exist. Two more defaults skew what you observe: many frameworks answer `OPTIONS` themselves and derive `HEAD` from a registered `GET`, so those methods often do not produce `405` even when nothing declares them. All of this is default behaviour, and it is normally overridable.

go deeper

for a junior

Know that using the wrong verb on a real path is a distinct failure from using an unknown path, and that a framework may report it as either 405 or 404.

for a middle

Explain why the answer depends on the router's lookup structure, and name the auto-handled methods that stop a mismatch from surfacing at all.

for a senior

Show the diagnostic habit: when a path you registered returns 404, check the method first, then the route grouping, then whether the request reached the application at all.

for a principal

Frame the choice as a disclosure decision - a uniform not-found hides the route table from probes, at a real cost in client debuggability - and decide per surface, not globally.

A request can miss the route table in two different ways, and which of them the framework can *see* decides what the client is told. This is one of the few places where an internal data-structure choice is visible from outside the process. ## Two ways a router can miss - **The path is unknown.** Nothing in the table describes that path under any method. There is no more information to give; the unmatched-route fallback answers. - **The path is known, the method is not.** The table has entries for that path, but none for the method that arrived. The framework holds a genuinely more useful fact: *this resource exists, you used the wrong verb*. A router whose lookup is path-first and method-second can distinguish those cases for free, because it lands on the path node and then fails to find the method. A router that hashes path and method together into one key cannot: both cases come back as "absent", and the second is reported as if it were the first. ## What each default tells the client | Default | What the client learns | Why a framework picks it | |---|---|---| | `405 Method Not Allowed` | The path exists; try another method | The router can see the distinction and passing it on is the most useful answer | | `404 Not Found` | Nothing useful about the path | The lookup key fuses path and method, or existence is deliberately not disclosed | | Handler runs and rejects | Whatever the handler says | Method checking was left to application code rather than the route table | The third row is worth naming as an anti-pattern rather than a design: when method filtering lives inside a handler instead of in the route declaration, the framework's built-in behaviour never fires, and the response shape is whatever that handler happens to write. ## Why some frameworks prefer 404 on purpose Answering `405` confirms the path is real. On a surface where the route table itself is sensitive - administrative endpoints, internal tooling, anything whose existence you would rather not advertise - a uniform `404` for both an unknown path and a wrong method removes that oracle. The cost is paid by legitimate clients, who lose a precise hint and see a misleading "does not exist" while debugging. Treat it as a deliberate tradeoff on a specific surface, not a default to apply everywhere. ## What the built-in 405 usually carries When a framework does produce the method-mismatch response, it typically fills in the header enumerating the methods it does have registered for that path, because the route table already holds exactly that set. This is why the built-in response is often more accurate than a hand-written one: it is generated from the registrations rather than from a comment somebody forgot to update. ## The two defaults that skew what you observe 1. **Automatic `OPTIONS`.** Many frameworks answer that method themselves from the route table rather than dispatching it, so it rarely reaches the mismatch branch. 2. **`HEAD` derived from `GET`.** Where a framework runs the registered `GET` handler and discards the body, a `HEAD` request against a `GET`-only route succeeds instead of producing a mismatch. Both are convenience defaults, both are usually switchable, and both explain "why did *that* method not give me the response I expected". ## Diagnosing it in practice The question a working engineer actually faces is "my client gets 404 for a path I am certain I registered". The ordered checks: - Did you register it for the method the client is using? A mismatch reported as `404` is the classic cause. - Is the path registered under a prefix or group that the request is not entering? - Did something in front of the application answer before it arrived - is there an access-log line at all? If the framework reports mismatches as `404`, note that you can usually change it, and that changing it is a disclosure decision as much as an ergonomics one. ## Talking about it in an interview Say what is true across frameworks rather than asserting one behaviour: the distinction exists only if the router indexes path and method separately, `405` is the common default where it does, `404` appears both as a structural consequence and as a deliberate choice, and auto-handled methods mean the observed behaviour is not a clean read of the route table.

  • Why would a team prefer 404 over the framework's default 405 for a method mismatch?
    Because `405` confirms the path exists. Where the route table itself is sensitive, answering `404` for both an unknown path and a wrong method removes that oracle. The cost falls on legitimate clients, who lose the hint that they simply used the wrong verb.
  • You add a handler for one method on a path and clients using another start seeing 405 instead of 404 - why?
    Registering any route makes the path visible to the router, so a path-first lookup now lands on it and reports a method mismatch. Before that, the path itself was absent and every request took the unmatched-path branch.
  • Why is the built-in method-mismatch response often more accurate than a hand-written one?
    It is generated from the route registrations themselves, so the set of methods it reports cannot drift from what the service actually serves. A hand-maintained list goes stale the first time someone adds a method and forgets.

saying these in an interview costs you the question

  • Says a method mismatch always produces 405 in every framework
  • Assumes a 404 for a method mismatch proves the route was never registered
  • Thinks the matched handler runs and rejects the method itself by default
  • Believes returning 404 rather than 405 is simply a framework bug
  • Expects OPTIONS or HEAD to produce 405 whenever no handler declares them