POST – WP rest api returns 404 only when author param is used

I am facing the same problem with a multisite running wordfence and jwt. I’m not sure if the solution here works for your case…

The following doesn’t work, it returns a 404 response. if we remove data.author It works without any problem.

jQuery.ajax({
    url: 'https://example.com/wp-json/wp/v2/posts',
    method: 'POST',
    crossDomain: true,
    dataType: 'json',
    data: { 'author':'1', 'title':'Hello world', 'content':'lorem ipsum', 'status':'publish' },
    beforeSend: function ( xhr ) {
        xhr.setRequestHeader( 'Authorization', 'Bearer <token>' );
    },
    success: function( data, txtStatus, xhr ) {
        console.log( xhr.status, data );
    }
});

This Will Work But I do not know Why:

fetch( 'https://example.com/wp-json/wp/v2/posts', {
    method: 'POST',
    body: JSON.stringify( {
        author: '1',
        content: 'lorem ipsum',
        title: 'Nice, it works!',
        status: 'publish'
    } ),
    headers: {
        'Content-Type': 'application/json',
        Authorization: 'Bearer <token>'
    }
} )
.then( res => res.json() )
.then( res => console.log( res ) );

I had three issues with Wordfence:

  • I could not get the application password working;
  • If we have 2FA enabled, authentication doesn’t work;
    I solved both of the above by using an editor user without 2FA nor app password;
  • Finally, not being able to install the author that brought me to this question.

Leave a Comment