1

I'm trying to create a route in my store, to receive notifications from an API, but every time I test the route I created, I get this error message No route was found matching the URL and request method

function notifications_cbt_func($request) {
     return $request;
}

add_action('rest_api_init', function () {
     register_rest_route( 'cbt-api/v1', '/notifications/', array(
       'methods' => 'GET',
       'callback' => 'notifications_cbt_func',
     ) );
});

when I make my call via postman always gives this error

GET: https://planetmusicexpress.com/wp-json/cbt-api/v1/notifications/?merchant_id=765&resource=\/orders\/MBR010131&topic=orders

this code of mine is inside a plugin that I created, I'm sure my plugin is working, because I have others called within the same file as this code that are working. Can anyone have any idea what I'm doing wrong?

I know my route works because it appears here https://planetmusicexpress.com/index.php/wp-json

what I don't know is to mount her call on get, or mount the regular expression so that it looks the same as it is on my get

EDIT

function notifications_cbt_func($request) {
     $merchant_id = $request->get_param( 'merchant_id' );
     $resource    = $request->get_param( 'resource' );

     // Get all the parameters:
     $params = $request->get_params();

     return json_encode($$params);
}

add_action('rest_api_init', function () {
     register_rest_route( 'cbt-api/v1', '/notifications/', array(
       'methods' => 'GET',
       'callback' => 'notifications_cbt_func',
     ) );
});

add_action('rest_api_init', function () {
     register_rest_route( 'cbt-api/v1', '/notifications/', array(
       'methods' => 'POST',
       'callback' => 'notifications_cbt_func',
     ) );
});

I made the changes suggested in the responses, but my route both get and post are returning null when I do this:

https://planetmusicexpress.com/wp-json/cbt-api/v1/notifications/?merchant_id=765&resource=MBR010131&topic=orders
3
  • 1
    I visited the endpoint and it worked fine? – Sally CJ Apr 10 '20 at 3:37
  • I managed to stop giving error, but I can't get what I pass in parameters, I put the regular expression, it keeps giving the error. – Marcius Leandro Apr 11 '20 at 11:47
  • 1
    I hope the answer helps? – Sally CJ Apr 11 '20 at 18:52
3

As I said in the comment, your endpoint worked fine for me.

And you said,

I managed to stop giving error, but I can't get what I pass in parameters, I put the regular expression, it keeps giving the error.

So not sure about that "regular expression" thing, but you can use $request->get_param() to get a specific parameter, or $request->get_params() to get all parameters in the request (e.g. the URL):

function notifications_cbt_func( $request ) {
    // Get specific parameters:
    $merchant_id = $request->get_param( 'merchant_id' );
    $resource    = $request->get_param( 'resource' );

    // Get all the parameters:
    $params = $request->get_params();

    //...
}

And note that in the above examples, the $request variable is a WP_REST_Request instance, so please check that link for the class methods, properties, etc.

You should also check the REST API handbook. :)

Update

In response to the comment or the edited question,

In your callback (notifications_cbt_func()), there's no need for the json_encode() because WordPress REST API endpoints indeed return a JSON-encoded string, i.e. a JSON response.

So just do return $params; or whatever that needs to be returned.

And about the "but my route both GET and POST are returning null" — That's because you used $$params which (is a null and) should be $params.

Also, there's no need to call register_rest_route() multiple times for the same route (/cbt-api/v1/notifications in your case). Just call it once with the third parameter being an array of endpoints:

register_rest_route( 'cbt-api/v1', '/notifications', array(
    array(
        'methods'  => 'GET',
        'callback' => 'notifications_cbt_func',
    ),
    array(
        'methods'  => 'POST',
        'callback' => 'notifications_cbt_func',
    ),
) );

And for the exact same callback (and parameters), you can simply supply an array of methods:

register_rest_route( 'cbt-api/v1', '/notifications', array(
    'methods'  => array( 'GET', 'POST' ),
    'callback' => 'notifications_cbt_func',
) );
2
  • Tanks, but I made the changes suggested in the responses, but my route both get and post are returning null – Marcius Leandro Apr 15 '20 at 16:42
  • Check the "Update" part. And btw, sorry, I was off-line until an hour ago. – Sally CJ Apr 22 '20 at 7:00

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.