How to get post with associated categories and tags names instead of ids with rest api?

When I copy/pasted your code I got an error message because enf_register_categories_names_field() wasn’t defined.

When I cleaned up your code and wrapped the register_rest_field() in a function called “enf_register_categories_names_field” it worked as expected.

function ag_filter_post_json($response, $post, $context) {
    $tags = wp_get_post_tags($post->ID);
    $response->data['tag_names'] = [];
    foreach ($tags as $tag) {
        $response->data['tag_names'][] = $tag->name;
    }
    return $response;
}
add_filter( 'rest_prepare_post', 'ag_filter_post_json', 10, 3 );

function enf_register_categories_names_field(){
    register_rest_field( 'post',
        'categories_names',
        array(
            'get_callback'    => 'enf_get_categories_names',
            'update_callback' => null,
            'schema'          => null,
        )
    );  
}

add_action( 'rest_api_init', 'enf_register_categories_names_field' );

function enf_get_categories_names( $object, $field_name, $request ) {
    $formatted_categories = array();
    $categories = get_the_category( $object['id'] );
    foreach ($categories as $category) {
        $formatted_categories[] = $category->name;
    }
    return $formatted_categories;
}