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: /?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 .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:
/?merchant_id=765&resource=MBR010131&topic=orders
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/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/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/wp-json/cbt-api/v1/notifications/?merchant_id=765&resource=MBR010131&topic=orders
Share
Improve this question
edited Apr 15, 2020 at 16:41
Marcius Leandro
asked Apr 10, 2020 at 1:35
Marcius LeandroMarcius Leandro
1231 silver badge7 bronze badges
3
- 1 I visited the endpoint and it worked fine? – Sally CJ Commented Apr 10, 2020 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 Commented Apr 11, 2020 at 11:47
- 1 I hope the answer helps? – Sally CJ Commented Apr 11, 2020 at 18:52
1 Answer
Reset to default 5As 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',
) );