• I am currently facing a difficulty with this endpoint i am developing. I always get a “rest_no_route” error while i have well installed the plugin and everything is live. Can you please tell me what the problem is? By the way, a post request that i created within the same code is well working…

    function custom_endpoint_get_user_info( $request ) {
    
        $username = $request['username']; // Get the username from the request
    
        $user_info = get_custom_user_id( $username );
    
        if ( $user_info ) {
    
            // User found, return the data in JSON format
    
            header( 'Content-Type: application/json' );
    
            $user_info_json = array(
    
                'id' => $user_info
    
            );
    
            //echo json_encode( $user_info );
    
        } else {
    
            // User not found, return an error response in JSON format
    
            header( 'HTTP/1.1 404 Not Found' );
    
            header( 'Content-Type: application/json' );
    
            echo json_encode( array( 'error' => 'User not found but working' ) );
    
        }
    
        // Return the retrieved information as a JSON response
    
        return rest_ensure_response( $user_info_json );
    
    }
    
    function custom_endpoint_get_user_infs() {
    
        register_rest_route(
    
            'custom/v1',
    
            '/user-info/(?P<username>[a-zA-Z0-9-_@.]+)', // Endpoint URL with a parameter for the username
    
            array(
    
                'methods' => 'GET',
    
                'callback' => 'custom_endpoint_get_user_info',
    
                'args' => array(
    
                    'username' => array(
    
                        'validate_callback' => 'rest_validate_request_arg', // Validate the username parameter
    
                    ),
    
                ),
    
            )
    
        );
    
    }
    
    add_action( 'rest_api_init', 'custom_endpoint_get_user_infs' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The code you’ve provided seems to define a custom REST API endpoint in WordPress. The endpoint is designed to retrieve user information based on the provided username. However, you’re encountering a “rest_no_route” error, which typically indicates that WordPress couldn’t find a matching route for the given endpoint. Let’s go through some troubleshooting steps to identify and fix the issue:

    1. Check the Endpoint URL: Make sure you are using the correct URL to access the custom endpoint. It should be something like: https://yourdomain.com/wp-json/custom/v1/user-info/username.
    2. Flush Permalinks: Sometimes, WordPress needs its permalinks to be flushed for new endpoints to work properly. Go to the WordPress admin dashboard, navigate to “Settings” > “Permalinks,” and simply save the current permalink structure without making any changes. This action flushes the rewrite rules and can help resolve routing issues.
    3. Namespace and Route: Double-check the namespace and route you’ve registered for your custom endpoint. In your code, you’ve registered the route under the namespace 'custom/v1' and route 'user-info/(?P<username>[a-zA-Z0-9-_@.]+)'.
    4. Confirm Plugin Activation: Ensure that the plugin containing this code is activated and active. Sometimes, issues arise if the plugin isn’t activated or if there are conflicts with other plugins.
    5. Debugging Output: You have comments around echo json_encode($user_info); and return rest_ensure_response($user_info_json);. Uncommenting these lines can help you debug the issue further by seeing the output directly. You can also use error_log to log messages to the debug log for more insight.
    6. Check for Typos: Double-check for any typos or syntax errors in your code. Even a small mistake can prevent the routing from working correctly.
    7. Plugin and Theme Conflicts: Sometimes, conflicts between plugins or themes can cause routing issues. Temporarily deactivate other plugins and switch to a default theme to see if the issue persists.
    8. WordPress Version: Ensure that your WordPress version is up to date. Sometimes, new features or changes in updates can impact how custom endpoints work.
    9. Server Configuration: Ensure that your server configuration supports pretty permalinks and mod_rewrite, as these are needed for custom endpoints to work correctly.
    10. Debugging Mode: Enable WordPress debugging mode to get more detailed error messages. Add the following lines to your wp-config.php file:
    Thread Starter danielkam

    (@danielkam)

    Thank you @abretado1985 for your reply.
    I have tried steps 1 to step 9 and still nothing
    As a reminder, it was working and suddenly no more working and it was also like that on another wordpress installation i had.
    Which line should i add in the wp-config.php to enable the debugging mode, please?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem with Get request’ is closed to new replies.