admin管理员组文章数量:1130349
I have the following code in my plugin, which during development has been working perfectly fine (called within rest_api_init).
// ?rest_route=bridge/v1/test-data/process/bbe_examples
register_rest_route(
'bridge/v1', '/(?P<participant>[a-zA-Z0-9-_]+)/process/(?P<section>[a-zA-Z0-9-_]+)', [
'methods' => 'GET',
'callback' => [ $this->api, 'process' ],
]
);
<My URL>/index.php?rest_route=/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce> Works fine, regardless of whether they're enabled.
<My URL>/wp-json/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce> returns 404 rest_no_route when called.
These are both currently called by some generated buttons, which are generated using rest_url( "bridge/v1/test-data/process/" ) (the section is appended to the string during display).
I'm not entirely sure what's going wrong here. I assumed I had to generate the full URL with rest_url(), but when directly called via the browser or API system the response is the same.
I have the following code in my plugin, which during development has been working perfectly fine (called within rest_api_init).
// ?rest_route=bridge/v1/test-data/process/bbe_examples
register_rest_route(
'bridge/v1', '/(?P<participant>[a-zA-Z0-9-_]+)/process/(?P<section>[a-zA-Z0-9-_]+)', [
'methods' => 'GET',
'callback' => [ $this->api, 'process' ],
]
);
<My URL>/index.php?rest_route=/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce> Works fine, regardless of whether they're enabled.
<My URL>/wp-json/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce> returns 404 rest_no_route when called.
These are both currently called by some generated buttons, which are generated using rest_url( "bridge/v1/test-data/process/" ) (the section is appended to the string during display).
I'm not entirely sure what's going wrong here. I assumed I had to generate the full URL with rest_url(), but when directly called via the browser or API system the response is the same.
1 Answer
Reset to default 1The problem is with the bbe_sortables&replace in the permalink:
<My URL>/wp-json/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce>
which results in an invalid route, where the supposedly query string is seen as part of the route:
/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce>
(the valid route for that URL is /bridge/v1/test-data/process/bbe_sortables)
and eventually the REST API throws the rest_no_route error.
To fix the problem, use add_query_arg() when generating the URL, to append the proper query string; e.g. with rest_url():
$url = add_query_arg( array(
'replace' => 'VALUE',
'_wpnonce' => 'VALUE',
), rest_url( 'bridge/v1/test-data/process/bbe_sortables/' ) );
/* Sample $url output:
a) Permalinks enabled
http://example/wp-json/bridge/v1/test-data/process/bbe_sortables/?replace=VALUE&_wpnonce=VALUE
b) Permalinks *not* enabled
http://example/index.php?rest_route=%2Fbridge%2Fv1%2Ftest-data%2Fprocess%2Fbbe_sortables%2F&replace=VALUE&_wpnonce=VALUE
*/
Or if you're certain that permalinks are always enabled on the site, then this would be fine:
rest_url( 'bridge/v1/test-data/process/bbe_sortables/?replace=VALUE&_wpnonce=VALUE' )
I have the following code in my plugin, which during development has been working perfectly fine (called within rest_api_init).
// ?rest_route=bridge/v1/test-data/process/bbe_examples
register_rest_route(
'bridge/v1', '/(?P<participant>[a-zA-Z0-9-_]+)/process/(?P<section>[a-zA-Z0-9-_]+)', [
'methods' => 'GET',
'callback' => [ $this->api, 'process' ],
]
);
<My URL>/index.php?rest_route=/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce> Works fine, regardless of whether they're enabled.
<My URL>/wp-json/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce> returns 404 rest_no_route when called.
These are both currently called by some generated buttons, which are generated using rest_url( "bridge/v1/test-data/process/" ) (the section is appended to the string during display).
I'm not entirely sure what's going wrong here. I assumed I had to generate the full URL with rest_url(), but when directly called via the browser or API system the response is the same.
I have the following code in my plugin, which during development has been working perfectly fine (called within rest_api_init).
// ?rest_route=bridge/v1/test-data/process/bbe_examples
register_rest_route(
'bridge/v1', '/(?P<participant>[a-zA-Z0-9-_]+)/process/(?P<section>[a-zA-Z0-9-_]+)', [
'methods' => 'GET',
'callback' => [ $this->api, 'process' ],
]
);
<My URL>/index.php?rest_route=/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce> Works fine, regardless of whether they're enabled.
<My URL>/wp-json/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce> returns 404 rest_no_route when called.
These are both currently called by some generated buttons, which are generated using rest_url( "bridge/v1/test-data/process/" ) (the section is appended to the string during display).
I'm not entirely sure what's going wrong here. I assumed I had to generate the full URL with rest_url(), but when directly called via the browser or API system the response is the same.
-
The problem is with the
bbe_sortables&replacein the pretty URL. If you change it tobbe_sortables?replace, that should work. – Sally CJ Commented Oct 29, 2018 at 13:59 - Oh damn, I didn't think of that! Can you write that as a reply so I can mark that as resolved? – soup-bowl Commented Oct 29, 2018 at 14:38
- I'm also well-aware that this almost completely violates the REST approach. I'm gradually integrating them, but this was - as it always is - a short term quick fix solution. Obviously it bit me, but there you go. – soup-bowl Commented Oct 29, 2018 at 15:36
- Not sure what you mean by "violates the REST approach"? But I already posted an answer for the original question. – Sally CJ Commented Oct 30, 2018 at 1:20
-
Went on a ramble. This is a paginated request, and the page identifer was also an argument like this. I changed it to
test-data/process/page/2to make it more clearer. Some of my older API practices were coming in. – soup-bowl Commented Oct 30, 2018 at 9:01
1 Answer
Reset to default 1The problem is with the bbe_sortables&replace in the permalink:
<My URL>/wp-json/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce>
which results in an invalid route, where the supposedly query string is seen as part of the route:
/bridge/v1/test-data/process/bbe_sortables&replace&_wpnonce=<thenonce>
(the valid route for that URL is /bridge/v1/test-data/process/bbe_sortables)
and eventually the REST API throws the rest_no_route error.
To fix the problem, use add_query_arg() when generating the URL, to append the proper query string; e.g. with rest_url():
$url = add_query_arg( array(
'replace' => 'VALUE',
'_wpnonce' => 'VALUE',
), rest_url( 'bridge/v1/test-data/process/bbe_sortables/' ) );
/* Sample $url output:
a) Permalinks enabled
http://example/wp-json/bridge/v1/test-data/process/bbe_sortables/?replace=VALUE&_wpnonce=VALUE
b) Permalinks *not* enabled
http://example/index.php?rest_route=%2Fbridge%2Fv1%2Ftest-data%2Fprocess%2Fbbe_sortables%2F&replace=VALUE&_wpnonce=VALUE
*/
Or if you're certain that permalinks are always enabled on the site, then this would be fine:
rest_url( 'bridge/v1/test-data/process/bbe_sortables/?replace=VALUE&_wpnonce=VALUE' )
本文标签: pluginsCustom REST API endpoint returns restnoroute when called via wpjson permalink
版权声明:本文标题:plugins - Custom REST API endpoint returns rest_no_route when called via wp-json permalink 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749223393a2335306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


bbe_sortables&replacein the pretty URL. If you change it tobbe_sortables?replace, that should work. – Sally CJ Commented Oct 29, 2018 at 13:59test-data/process/page/2to make it more clearer. Some of my older API practices were coming in. – soup-bowl Commented Oct 30, 2018 at 9:01