First of all, you registered the route “latest-post”, but the URL you used is “latest-posts”.
Second: In your “get_latest_post” function, you use the variable $category, but it is set nowhere.
Fix the function like this:
function get_latest_post ( $params ){
if(isset($params['category'])){
$post = get_posts( array(
'category' => $params['category'],
'posts_per_page' => 1,
'offset' => 0
) );
if( empty( $post ) ){
return new WP_Error( 'no_post_found', 'there is no post in this category', array( 'status' => 404 ) );
}
$returnage = new WP_REST_Response( array('post_title' => $post[0]->post_title) );
$returnage->set_status( 200 );
return $returnage;
} else {
return new WP_Error( 'no_category_given', 'please add category parameter', array( 'status' => 404 ) );
}
}
(The WP_Errors and WP_REST_Response ensure that you get a valid JSON REST API response from your function)