6

I am following this tutorial to create custom end points to WP-API .

I am always getting this error on hitting /wp-json/custom-plugin/v2/get-all-post-ids/ on postman to test :

{
    "code": "rest_no_route",
    "message": "No route was found matching
    the URL and request method ", 
    "data": {
        "status": 404
    }
}

I have created a custom-plugin.php file in /plugins/custom-plugin/ directory .

<?php
    if ( ! defined( 'ABSPATH' ) ) exit;

    add_action( 'rest_api_init', 'dt_register_api_hooks' );

    function dt_register_api_hooks() {    

        register_rest_route( 'custom-plugin/v2', '/get-all-post-ids/', array(
            'methods' => 'GET',
            'callback' => 'dt_get_all_post_ids',
            ) 
            );
    }
    // Return all post IDs
    function dt_get_all_post_ids() {
        if ( false === ( $all_post_ids = get_transient( 'dt_all_post_ids' ) ) ) {
            $all_post_ids = get_posts( array(
                'numberposts' => -1,
                'post_type'   => 'post',
                'fields'      => 'ids',
            ) );
            // cache for 2 hours
            set_transient( 'dt_all_post_ids', $all_post_ids, 60*60*2 );
        }
        return $all_post_ids;
    }
?>
Milap
  • 6,915
  • 8
  • 26
  • 46
Divyanshu Jimmy
  • 2,542
  • 5
  • 32
  • 48
  • Did you ever get this? It seems that sometimes `register_rest_route` just doesn't work sometimes? – Ben Racicot May 20 '16 at 23:26
  • 2
    @BenRacicot yes this works always , you can check a simple implementation here http://wordpress.stackexchange.com/a/227127/92345 – Divyanshu Jimmy May 22 '16 at 08:09
  • 1
    If it did not work, try resaving permalinks. – butlerblog Sep 17 '20 at 16:39
  • I've had similar problem and was able to figure it out by tracing it down step by step. Try first request /wp-json/. Do you find your routes and endpoints in the json? If yes, try /wp-json/custom-plugin/v2. What do you use in that json? – mikryz Dec 30 '20 at 07:10
  • Did you find the answer to this? I have the same problem here – Diogenes Oliveira Junior Sep 23 '21 at 04:36
  • @DiogenesOliveiraJunior try checking this https://wordpress.stackexchange.com/questions/223757/how-to-use-wp-rest-api-to-login-user-and-get-user-data-for-android-app/227127#227127 – Divyanshu Jimmy Sep 27 '21 at 10:32

2 Answers2

3

Ensure your callback to add_action( 'rest_api_init', 'dt_register_api_hooks' ); is being run.

In my case, my callback was not being called because I was using add_action('rest_api_init', ...) too late; the action had already fired. As in, my call to register_rest_route() never even happened.

Tyler Collier
  • 11,489
  • 9
  • 73
  • 80
1

I hope my answer can be useful to some as well.

For a very similar problem, while I was designing an API in WordPress, I also got the same "code": "rest_no_route",... error on some websites and not on other ones. I traced it back to the fact that POST requests were transformed into GET requests so they weren't recognized by my plugin. The conversion from POST to GET was made before WordPress even started. I was able to pinpoint the problem and solve it by adding the following header, as explained in details here:

headers: { 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8' }
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101