1

I am using Wordpress 4.9.5 and I want to create a custom endpoint like the following:

<?php
add_action('rest_api_init', 'miningRigsRoutes');

function miningRigsRoutes()
{
    register_rest_route('miningRigs/v1', 'createRig', array(
        'methods' => WP_REST_SERVER::CREATABLE,
        'callback' => 'createMiningRig',
    ));

    register_rest_route('miningRigs/v1', 'allRigs', array(
        'methods' => WP_REST_SERVER::READABLE,
        'callback' => 'allMiningRigs',
    ));
}

function createMiningRig($data)
{
    //var_dump($data);
    //print_r($data);
    return "Thanks for using the API";
}

function allMiningRigs() {
    return "All mining Rigs";
}

The url I am using is:

http://localhost/wordpress-site/wp-json/miningRigs/v1/createRig/

However, I get the following in return for the post-route:

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

The get-route is working perfectly fine!

I am currently do not use any third party plugins.

Any suggestions how to debug this error for the post-route? From my code, everything looks alright!

I appreciate your replies!

  • with CREATABLE this endpoints works only with POST. you can use e.g. "methods" => "GET, POST", to use it with GET and POST. – mmm Apr 22 '18 at 7:45
3

You should not GET on a POST only rest callback. The code below creates a POST call only

register_rest_route('miningRigs/v1', 'createRig', array(
    'methods' => WP_REST_SERVER::CREATABLE,
    'callback' => 'createMiningRig',
));

Check http://localhost/wordpress-site/wp-json/miningRigs/v1 which shows you can only POST on createRig

I just copy/paste you code + added the https://pressupinc.com/blog/2013/07/minimum-viable-wordpress-plugin/ then enabled the plugin on 4.9.5 and it works like expected on a WP 4.9.5

  • i think the combination of mmm comment and your answer will give a good answer. Right now it is not clear from the question how POST is related here, so maybe worth explaining further. – Mark Kaplun Apr 22 '18 at 9:07
  • I added your code snippet. 'WP_REST_SERVER::CREATABLE' is equivalent with a HTTP POST when visiting the mentioned URL – Clemens Tolboom Apr 22 '18 at 9:32
  • Curious about permissions etc I read the API of which developer.wordpress.org/rest-api/extending-the-rest-api/… just extends WP_REST_Controller which probably safes you from building your own GET all and filtering conditions. – Clemens Tolboom Apr 22 '18 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.