• Resolved dylanesque43

    (@dylanesque43)


    0

    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
      }
    }

    Is it because I don’t have a custom content type set up for the $request argument? Also, any guidance towards resources/tutorials that cover advanced WP REST API custom endpoint would be really appreciated, because I’m having a hard time finding examples beyond the most basic uses

    • This topic was modified 5 years, 9 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    You are not specifying any “args” when registering your route, so the callback’s $request is not passed any data. Doesn’t jive with the error message, but error messages are often misleading.

    Also be sure authentication is squared away or POST requests will fail no matter how well the endpoint is coded.

    All I can suggest for guidance beyond the REST API Handbook is to study the default endpoint controller classes as good examples to follow.

    Thread Starter dylanesque43

    (@dylanesque43)

    That makes a lot of sense, I will pursue that, thanks for pointing it out!

    Hi,
    I’m defining custom endpoint route using controller method, which is extending WP_REST_Controller. I’ve used this code in a custom file called api.php in my active theme.

    class Slug_Custom_Route extends WP_REST_Controller {
    
        public function register_routes() {
            register_rest_route('my-route', 'my-phrase', array(
                'methods' => 'POST',
                'callback' => 'custom_phrase',
                    )
            );
        }
    
        public function custom_phrase(WP_REST_Request $request) {
            $param = $request['param'];
        return rest_ensure_response('Hello World! This is my first REST API' . $param);
        }
    
    }
    add_action('rest_api_init', 'register_routes');

    It’s populating the result – 404 rest_no_route.

    As I’m new to all of this, Kindly help me to resolve.

    • This reply was modified 5 years, 5 months ago by pramo15.

    Hi! I’m also facing the same issue. Just to add ups, the same post request work for one website and the same post request gives 404 rest_no_route for another website. I faced this problem for 3 websites and for the rest 500+ websites it is working fine.
    So the problem is only with POST routes. I even used “args” but it didn’t help out.

    ` add_action( ‘rest_api_init’, function () {
    register_rest_route( ‘wc/v3’, ‘/my-route’, array(
    ‘methods’ => ‘POST’,
    ‘callback’ => ‘my_action’,
    ) );
    } );

    function my_action(WP_REST_Request $request) {

    $req = $request->get_json_params();
    //my Code
    return rest_ensure_response($req);

    }

    @dylanesque43 Were you able to sort this out? I’m having the same issue where the GET request works but when I create the route for the POST, I get the 404. When I look at the available routes on wp-json, it shows “GET” regardless of whether I set 'method' => \WP_REST_Server::READABLE or ALLMETHODS. It just shows up as a GET

    Thread Starter dylanesque43

    (@dylanesque43)

    kartboy16

    (@kartboy16)

    I’m running it on localhost. The weird part is some of my POST routes work but anything I create now just shows up as GET on wp-json even with setting \WP_REST_SERVER::CREATABLE

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

The topic ‘WP REST API Custom POST Endpoint Not Working, 404 error’ is closed to new replies.