Title: REST examples
Last modified: October 6, 2020

---

# REST examples

 *  Resolved [Beee](https://wordpress.org/support/users/beee/)
 * (@beee)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/rest-examples/)
 * Hey John, like your plugin a lot.
 * I now have the need for dynamic adding/editing of redirects.
    I wanted to do 
   this with the REST API but I can’t seem to get it to work.
 * I have tried using `wp_remote_get()` with just the URL and I also tried some 
   cUrl variations (which is normally my goto solution), but I can’t get any proper
   response with any of them.
 * Are there any php/curl examples available somewhere ?
 * I just want to achieve a simple redirect from /slug1/ to /slug2/.
    -  This topic was modified 5 years, 8 months ago by [Beee](https://wordpress.org/support/users/beee/).

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [John Godley](https://wordpress.org/support/users/johnny5/)
 * (@johnny5)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/rest-examples/#post-13496451)
 * It uses the standard WordPress REST API, and there are guides and examples on
   the internet that should help:
 * [https://developer.wordpress.org/rest-api/using-the-rest-api/](https://developer.wordpress.org/rest-api/using-the-rest-api/)
   
   [https://developer.wordpress.org/reference/functions/rest_do_request/](https://developer.wordpress.org/reference/functions/rest_do_request/)
   [https://redirection.me/developer/rest-api/](https://redirection.me/developer/rest-api/)
 *  Thread Starter [Beee](https://wordpress.org/support/users/beee/)
 * (@beee)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/rest-examples/#post-13498089)
 * 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, 8 months ago by [Beee](https://wordpress.org/support/users/beee/).
 *  Plugin Author [John Godley](https://wordpress.org/support/users/johnny5/)
 * (@johnny5)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/rest-examples/#post-13498303)
 * > I then tried to add a paramater to try and understand the syntax/setup.
 * What are you trying to do with that set_param? The API document I linked to shows
   the syntax for filtering.
 * [https://redirection.me/developer/rest-api/#api-Redirect-GetRedirects](https://redirection.me/developer/rest-api/#api-Redirect-GetRedirects)
   
   [https://developer.wordpress.org/rest-api/requests/](https://developer.wordpress.org/rest-api/requests/)
 *  Thread Starter [Beee](https://wordpress.org/support/users/beee/)
 * (@beee)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/rest-examples/#post-13498668)
 * 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/](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…
 *  Plugin Author [John Godley](https://wordpress.org/support/users/johnny5/)
 * (@johnny5)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/rest-examples/#post-13498809)
 * If you look at [https://redirection.me/developer/rest-api/#api-Redirect-GetRedirects](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](https://wordpress.org/support/users/beee/)
 * (@beee)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/rest-examples/#post-13500254)
 * 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.
 *  Plugin Author [John Godley](https://wordpress.org/support/users/johnny5/)
 * (@johnny5)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/rest-examples/#post-13519239)
 * 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.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘REST examples’ is closed to new replies.

 * ![](https://ps.w.org/redirection/assets/icon-256x256.jpg?rev=983639)
 * [Redirection](https://wordpress.org/plugins/redirection/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/redirection/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/redirection/)
 * [Active Topics](https://wordpress.org/support/plugin/redirection/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/redirection/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/redirection/reviews/)

 * 7 replies
 * 2 participants
 * Last reply from: [John Godley](https://wordpress.org/support/users/johnny5/)
 * Last activity: [5 years, 8 months ago](https://wordpress.org/support/topic/rest-examples/#post-13519239)
 * Status: resolved