Thread Starter
Beee
(@beee)
Thanks for the reply.
I have tried using several examples, but didn’t get there yet. I noticed 1 incorrect thing, which (when fixed) resulted in getting a result on a default get request, when I used this code.
$request = new WP_REST_Request( 'GET', '/redirection/v1/redirect' );
$response = rest_do_request( $request );
echo '<pre>'; var_dump($response->get_data); echo '</pre>'; exit;
I then tried to add a paramater to try and understand the syntax/setup.
$request->set_param( 'status', 'enabled' );
But $response->get_data stays the same when I change enabled to disabled.
I think my parameter syntax might off on how to add parameters, but I can’t find any info that explains it to me.
Would appreciate some help if possible.
-
This reply was modified 5 years, 5 months ago by
Beee.
Thread Starter
Beee
(@beee)
I tried adding the parameter like this:
$request = new WP_REST_Request( 'GET', '/redirection/v1/redirect' );
$request->set_param( 'status', 'disabled' );
$response = rest_do_request( $request );
$data = $response->get_data();
which I got from https://developer.wordpress.org/reference/classes/wp_rest_request/set_param/
I also tried $request->set_query_params( [ 'status', 'disabled' ] );
As I said I have seen/read the page you linked, but it seems I don’t quite get the needed/required syntax.
I tried adding them to the request URL directly but it gave no changes in the $data variable.
$request = new WP_REST_Request( 'GET', '/redirection/v1/redirect/?filterBy[status]=disabled' );
$request = new WP_REST_Request( 'GET', '/redirection/v1/redirect/?filter[status]=disabled' );
$request = new WP_REST_Request( 'GET', '/redirection/v1/redirect/?status=disabled' );
All 3 gave the error: rest_no_route.
I think it’s just the syntax I have wrong… If I can get past that…
If you look at https://redirection.me/developer/rest-api/#api-Redirect-GetRedirects you will see that there is no parameter called status.
I’ve never used rest_do_request but using the above documentation I would imagine it would be:
$request->set_param( 'filterBy[status]', 'disabled' );
If you’re not sure what is happening then you can also add debugging information to WordPress to see what parameters are being received.
Thread Starter
Beee
(@beee)
In the last code block you see I tried several variations but they didn’t work.
As you can see I am approaching it differently than you probably would.
I’ve been tinkering with it for hours and I can’t get it right.
That’s why I was hoping you could provide an example with a simple query.
You say you never use rest_do_request. Can you maybe share an example you use ? I think it would help a lot…
Edit: I tried your suggestion, didn’t work.
Sorry, I don’t have any examples. The API is used by the JavaScript client, so the code that is used is not applicable for PHP.
This question is not really related to Redirection but to the WordPress REST API. You may find it better to ask the question in a different forum.