1
votes

I'm writing a plugin, and I'm having some trouble with the custom endpoint that takes in data from the front-end of the app (Angular), and will pass it another function to filter some data. I have the GET request to the product database working just fine, but the POST just returns a 404 error (an empty array if I test the endpoint in Insomnia). Solutions I've tried include making sure that pretty permalinks were set, switching themes, all to no avail. I have also checked the wp-json file, and it is showing up in there. The code for this endpoint:

function get_awesome_params(WP_REST_Request $request) {
  // question attributes from angular code
  $parameters = $request->get_params();
  return new WP_REST_Response($parameters, 200);
}

add_action('rest_api_init', function() {
  register_rest_route('awesome/v1', '/awesomeparams', array(
    'methods' => WP_REST_Server::CREATABLE,
    'callback' => 'get_awesome_params',
    'permission_callback' => function () {
      return true;
    }
  ));
});

EDIT: Error message on visiting page is:

{
  code: "rest_no_route",
  message: "No route was found matching the URL and request method",
  data: {
    status: 404
  }
}
1

1 Answers

2
votes

This wasn't working because I wasn't passing any arguments in, and a small change in the register function fixed that:

add_action('rest_api_init', function() {
  register_rest_route('awesome/v1', 'awesomeparams', array(
    'methods' => WP_REST_SERVER::CREATABLE,
    'callback' => 'get_awesome_params',
    'args' => array(),
    'permission_callback' => function () {
      return true;
    }
  ));
});