Problem with Get request
-
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)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Problem with Get request’ is closed to new replies.