REST API: No route was found matching the URL and request method

It’s because of how you’ve defined the accepted methods:

'methods'   => '\WP_REST_Server::CREATABLE ',

You shouldn’t have quotes around it. WP_REST_Server::CREATABLE is a string that equals 'POST', but by putting quotes around it you’re literally setting the method as '\WP_REST_Server::CREATABLE', which is not a valid HTTP method. You can see this in the response to the namespace endpoint.

Set it like this:

'methods' => WP_REST_Server::CREATABLE

Or, if your file is using a PHP namespace, like this:

'methods' => \WP_REST_Server::CREATABLE

Or add this to the top of the file:

use WP_REST_Server;

Then make sure that when you’re accessing the route directly, that you’re using the correct method. If you’re using WP_REST_Server::CREATABLE then the endpoint will only respond to POST requests, so GET requests will return a 404, which includes when you access it via the browser.

Leave a Comment