I’ve downloaded the sample form pages/function from git - https://github.com/cloudflare/submit.pages.dev
I haven’t made any changes and just tried to get it working via wrangler locally, but no matter what I try, when I POST via the submit button I get a 405 Method Not Allowed error.
I’ve tried creating my own very basic form with a function, but same 405 error everytime.
I’ve used Wrangler to test locally and publish to the CF pages service, same outcome
It sounds like to me that your Function isn’t actually running, and you’re hitting the asset server to fetch a static asset, which doesn’t accept POST requests so throws a 405.
Are you certain the function is running on the right URL, and the form is posting to that URL? Have you tried to wrangler pages tail with some logs? Or added another debug function on GET that echoes something back to verify your Functions are working as expected?
Hey guys, this looks to be a really weird Cloudflare issue!
I just made a simple copy/paste of the code to a new Function in another path and it simply works.
But the same code in the older path still fails with 405.
And this occurs in both, localhost and production.
I don’t know what is going on, but if anyone else is running into this issue, just try to copy/paste your function to a new one in a new path.
Really weird!
$ curl -X POST https://a47c61f2.functions-ignore-test.pages.dev/something
Function not something.html
$ cat functions/something.js
export function onRequest() {
return new Response('Function not something.html');
}
$ curl -X POST https://a47c61f2.functions-ignore-test.pages.dev/post
https://community.cloudflare.com/t/cf-pages-functions-wrangler-dev-405-method-not-allowed-locally/442767
$ cat functions/post.js
export function onRequestPost() {
return new Response('https://community.cloudflare.com/t/cf-pages-functions-wrangler-dev-405-method-not-allowed-locally/442767');
}
As @cherryjimbo mentioned, this error happens when your Function is not fired.
Thanks for that, great spot… This now works as expected.
For anyone else having this issue, only the static assets are deployed from the attribute e.g. wrangler pages dev and as Walshy advised the functions are only deployed from the root of the current directory your console/command window.
If you look at my previous screenshots, you will see I was in the parent directory, even though I was passing the full path to the attribute.
I have a static website hosted with Pages and requesting some KV data through Pages Functions.
Every GET request to functions made with onRequestGet is working well, as expected.
But a POST request from a form submission to a function made with onRequestPost is failing with the HTTP status 405: Method not allowed.