How to include file attachment in ajax submission via the rest_api?

You can really use FormData just like you could use it with the old admin-ajax.php route, but:

  1. Set processData and contentType to false.

  2. Set the method to POST and make sure your REST API route supports the POST method.

    $('#create-book-form').submit(function (e) {
    
    //  var $this = $(this); // what's this?
        e.preventDefault();
    
        var data = new FormData( this ); 
    
        data.append( 'rtype', 'create' ); // add extra param
    
        $.ajax({
    
            url: BOOK_SUBMITTER.root + 'rfp-books/v1/books',
            data: data, //$.param(data),
            processData: false,
            contentType: false,
            method: 'POST',
            beforeSend: function ( xhr ) {
                xhr.setRequestHeader( 'X-WP-Nonce', BOOK_SUBMITTER.nonce );
            },
            success: function ( data ) {
                console.log( data ); // I added just for testing purposes.
            },
        });
    });
    

Then in your REST API endpoint callback, just use the $_FILES to get the uploaded file, e.g. $_FILES['file']. For other parameters, you can use $request->get_param(), e.g. $request->get_param( 'rtype' ).

Additionally or to other readers, you should have a file upload input in your form, e.g. <input type="file" name="file" />, but the name can be anything unless if you’re creating an attachment using the default wp/v2/media route.