Cannot modify header error when creating rest api in wordpress

Solution:

You need to create custom AJAX action. You will find everything you need on the codex : https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

Or you can use the WordPress REST API plugin. It’s allow you to create your own API endpoint. https://wordpress.org/plugins/rest-api/

EDIT I give you a little example, with the jQuery ajax code to call it.

add_action('wp_ajax_XXXXXX', 'ajax_XXXXXX');
add_action('wp_ajax_nopriv_XXXXXX', 'ajax_XXXXXX');
function ajax_XXXXXX() {
    header('Content-Type: application/json');
    echo json_encode(array(
        'text' => "Lorem ipsum dolor ...",
        'time' => time(),
        'user_id' => get_current_user_id()
    ));
    die();
}
$.ajax({
    url : "/wp-admin/admin-ajax.php",
    method : "POST",
    data : {
        action : "XXXXXX"
    },
    success : function(datas) {
        console.log(datas);
    }
});