DEVESSENTIALS

HTTP 405 Method Not Allowed

405 means the URL exists but doesn't accept that HTTP method, for example DELETE /users when the collection only supports GET and POST.

4xx · Client errorRFC 9110 §15.5.6

405 Method Not Allowed

The method is known to the server but not supported by the target resource.

Who fixes it: Either side. Either the client used the wrong method, or the server routes the request to something that doesn't handle it (often a static file handler).

What 405 means

RFC 9110 requires the server to send an Allow header listing the methods the resource does support, so a 405 is self-documenting: the client can read Allow: GET, POST and fix its request.

Don't confuse it with 501 Not Implemented, which means the server doesn't support the method for any resource at all.

Source: RFC 9110 §15.5.6.

Common causes

  • A form or fetch() posting to a static file. nginx serves static files only for GET and HEAD, so POST /index.html gets 405 Not Allowed. It happens when an API request misses the proxy location and falls through to the static root.
  • Next.js App Router: route.ts doesn't export a function for that method, e.g. only GET is exported and the client sends POST. Next.js responds 405 automatically.
  • CORS preflight: the browser sends OPTIONS before a cross-origin PUT, DELETE or JSON POST. If nothing handles OPTIONS, some frameworks return 405 and the browser reports a CORS error.
  • A redirect changed the method: a POST that gets a 301 or 302 (often a trailing-slash redirect) is re-sent as GET, which may not be allowed at the target.
  • The wrong method by mistake: PUT vs PATCH, or GET to a search endpoint that only takes POST.
  • IIS: the WebDAV module intercepts PUT and DELETE and returns 405 before your ASP.NET app sees them.

How to fix a 405

Ask the server what it allows

The Allow header on the 405 lists the supported methods. Some servers also answer OPTIONS with it.

curl -i -X DELETE https://api.example.com/users

HTTP/2 405
allow: GET, POST

curl -i -X OPTIONS https://api.example.com/users

nginx: make sure API calls reach the app

If a POST gets nginx's own 405 Not Allowed page, it was handled as a static file. Check that the URL matches the proxy location, including the trailing slash.

location /api/ {
    proxy_pass http://127.0.0.1:3000;
}

location / {
    root /var/www/app/dist;
    try_files $uri $uri/ /index.html;
}

Next.js Route Handlers

Export one function per method you support. Anything else gets a 405.

// app/api/items/route.ts
export async function GET() {
  return Response.json(await listItems());
}

export async function POST(request: Request) {
  const item = await createItem(await request.json());
  return Response.json(item, { status: 201 });
}
// No DELETE export: DELETE /api/items returns 405

Express: send a proper 405

Express returns 404 for unsupported methods by default. router.route() with a final .all() gives you a correct 405 with Allow. Express answers HEAD for any GET route, so include it.

app.route('/api/items')
  .get(listItems)
  .post(createItem)
  .all((req, res) => {
    res.set('Allow', 'GET, HEAD, POST');
    res.status(405).json({ error: 'method_not_allowed' });
  });

Headers that matter with 405

Allow
Required in a 405. An empty value is valid and means the resource currently allows no methods, e.g. when it's disabled by configuration.
Access-Control-Allow-Methods
The CORS equivalent for preflight responses. Missing methods here cause browser CORS errors, not a 405.

405 vs similar status codes

  • 404 Not Found: Nothing at the URL at all. Express uses 404 where 405 would be more accurate.
  • 501 Not Implemented: The server doesn't recognise or support the method for any resource.
  • 403 Forbidden: The method is supported, but this client isn't allowed to use it.

Related status codes

Looking for another code? Search the full HTTP status codes list, or read which code your API should return in the REST API status codes guide.

Learn more

Browse all developer guides →

Frequently Asked Questions

What is the difference between 405 and 404?▾

404 means nothing exists at that URL. 405 means the URL exists but doesn't accept the method you used. Express is a common source of confusion: it returns 404 with Cannot POST /path for an unsupported method unless you add a 405 handler.

Why does nginx return 405 Not Allowed for my POST request?▾

nginx's static file handler only supports GET and HEAD. A POST that lands in a location serving files, instead of being proxied to your app, gets a 405. Check that the URL matches your proxy_pass location, including trailing slashes.

Does a 405 response need an Allow header?▾

Yes. RFC 9110 says the origin server must send Allow with the methods the resource currently supports. An empty Allow header is valid and means no methods are currently allowed.