1

I used following script for REST API for POST Method in my existing method. this message is showing ""No route was found matching the URL and request method."," where is the error. I have already "save change" in the Permalink. various way verify my code which is corrected. same error is showing

class Class_dashboard { 

    public function __construct() {
       $this->display_latest_post();
       add_action('rest_api_init', array($this, 'lincense_register_routes'));   
    }  

    public function lincense_register_routes() {
        register_rest_route('license/v2', '/verify/', array(
            'methods' => 'POST',
            'callback' => array($this, 'verify_license_key'),
            'permission_callback' => '__return_true',
        ));
    }

    public function verify_license_key(WP_REST_Request $request) {
        header("Access-Control-Allow-Origin: *");  // Allow requests from any origin
        header("Access-Control-Allow-Methods: POST, GET");
        header("Access-Control-Allow-Headers: Content-Type");

        $license_key = sanitize_text_field($request->get_param('license_key'));
        //$license_key === '06078F5C173B8B98BFA8';
        global $wpdb;
        $table_name = $wpdb->prefix . 'woo_license_item';
    
        $result = $wpdb->get_row(
            $wpdb->prepare("SELECT * FROM $table_name WHERE licenseKey = %s", $license_key)
        );

        if ($result) {
            // Check if the license is active and not expired
            //if ($result->keystatus === 'close' && (empty($result->expiration_date) || strtotime($result->expiration_date) > time())) {
            if ($result->keystatus === 'close') {
                return new WP_REST_Response(array('status' => 'valid', 'message' => 'License is valid'), 200);
            } else {
                return new WP_REST_Response(array('status' => 'expired', 'message' => 'License has expired'), 200);
            }
        } else {
            return new WP_REST_Response(array('status' => 'invalid', 'message' => 'License key not found'), 200);
        }
    }
}

I cant find the error. why showing error again and again

4
  • Have you first of all verified whether your lincense_register_routes method gets invoked at all? Commented Oct 9, 2024 at 8:09
  • My current script is working. Thank @C3roe Commented Oct 9, 2024 at 9:16
  • Just to be clear, you are calling new Class_dashboard somewhere, too, right? Can you also show the exact route (domain redacted) that you are testing? Commented Oct 9, 2024 at 13:23
  • @ChrisHaas I was wrong way initiate new Class_dashboard. Commented Oct 15, 2024 at 4:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.