0

I have an endpoint like this:-

function foo_hook() {
register_rest_route( 'sample_namespace/v1', '/author/(?P<author_name>[a-zA-Z0-9-_\%]+)', array(
'methods' => 'GET',
'callback' => 'bar_func',
) );
} 

add_action( 'rest_api_init', 'foo_hook');

function bar_func( WP_REST_Request $request ) {
$param = urldecode($request->get_param( 'author_name' ));
$result = array();
$result['author_name'] = $param;
return $result;
}

When I try to access it passing the author name to the API it works just fine, here is the URL I hit

http://sample.com/wp-json/sample_namespace/v1/author/dave_the_author

It return this result

{"author_name":"dave_the_author"}

But when the string has a forward slash "/" (e.g. - dave/the_author) and is passed as an argument, then it always returns 404 (see url below).

http://sample.com/wp-json/sample_namespace/v1/author/dave/the_author

If I convert "/" character to urlencode and it becomes %2F , even then this is returning a 404 (see url below).

http://sample.com/wp-json/sample_namespace/v1/author/dave%2Fthe_author

P.S.: although "/" is assumed to be a part of url so it shouldn't be used, but this case also arise when let's say you want to pass a base64 encoded long string, that will have a lot of forward slashes and that as a string needs to be passed as parameter.

  • your regex doesn't allow / then try with /author/(?P<author_name>[a-zA-Z0-9-_\%/]+) – mmm Jun 27 '18 at 21:07

Your Answer

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

Browse other questions tagged or ask your own question.