• I am working on an application that serves an Angular app in a WP plugin. The flow works like so:

    -User answers questions in a brief survey on the Angular front-end, which makes a POST request to the WP custom POST endpoint with the survey answers.

    -The callback function attached to the WP POST endpoint takes those answers, filters them (the plugin is essentially a Woocommerce product filter), and returns a filtered product list.

    -The Angular app makes a GET request to the WP endpoint, and renders that filtered product list.

    I’m doing something wrong, because all the parts are working individually: The Angular HTTP requests, filtering logic, and custom endpoints (when tested via Insomnia or with hard-coded arguments) are doing what they’re supposed to when tested in isolation, but when I test the app out in staging, the render just silently fails.

    I’m pretty new to WordPress, but I’ve pored over the REST API docs, and they left me with a LOT of questions, namely:

    -How do I test this setup and ensure that the proper request data is indeed reaching the endpoint?

    -Am I doing this correctly? Should I have separate POST and GET logic, or is just having the POST more than enough? My code looks like this:

    add_action('rest_api_init', function() {
      register_rest_route('awe/v1', '/awesomeproducts', array(
        'methods' => 'POST',
        'callback' => 'get_awesome_params',
        'args' => array(),
        'permission_callback' => function () {
          return true;
        }
      ));
    });
    
    function get_awesome_params( WP_REST_Request $request ) {
      $parameters = $request->get_params();
    
      // Very long filtering logic takes the $parameters and turns it
      // into $finalProducts, an array of objects
    
      return new WP_REST_Response($final_products, 200);
    }

    I’ve spent a lot of time looking for ansers on here, Stack Overflow, WordPress Stack Exchange, etc. Any help would be greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @dylanesque43,

    Find the responses in italic format below:

    • How do I test this setup and ensure that the proper request data is indeed reaching the endpoint?
      If it is about testing the endpoint, you can use Postman App for it.
    • Am I doing this correctly? Should I have separate POST and GET logic, or is just having the POST more than enough?
      Actually, having the POST and GET logic in a single method is not a problem but it’s not standard practice. I will suggest to keep them separate. What you can do it, you can send POST method with the parameters to be updated in WP database and depending upon it’s response, you can send another API request to get the relevant data.
    • Please let me know your thoughts.

    Thread Starter dylanesque43

    (@dylanesque43)

    “If it is about testing the endpoint, you can use Postman App for it.”

    I’ve already used Insomnia for testing the basics, which is functionally equivalent to Postman. What neither of them can tell me for sure is whether or not WP is processing the request sent to that endpoint, only what it returns when X is passed in as args.

    “Actually, having the POST and GET logic in a single method is not a problem but it’s not standard practice. I will suggest to keep them separate. What you can do it, you can send POST method with the parameters to be updated in WP database and depending upon it’s response, you can send another API request to get the relevant data.”

    So, because it’s a plugin that will be installed on many sites where the only constant I can count on is having the Woocommerce plugin installed, I can’t rely on the WP database. Sorry, I should have made that more clear in the original post, editing that now.

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

The topic ‘Custom REST Endpoint In Plugin’ is closed to new replies.