JavaScript authenticated requests

In case you are using any client-side JavaScript framework, authorization headers can be sent for the desired interaction with WP API. The DELETE request would then be sent by another jQuery.ajax() method like this:

$.ajax({ 
    url: 'http://something/wp-json/wp/v2/posts/25', 
    method: 'DELETE', 
    crossDomain: true, 
    beforeSend: function ( xrh ) { 
        xrh.setRequestHeader( 'Authorization', 'Basic ' +          Base64.encode( 'username:password' ) ); 
    }, 
    success: function( data, txtStatus, xrh ) { 
        console.log( data ); 
        console.log( xrh.status ); 
    } 
}); 

In this example, the BASE64 is considered to be an object used for encoding a string.

This is a way of cross-browser encoding of a string within JavaScript.

The Authorization header was used ...

Get Learning WordPress REST API now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.