Rest API basic auth not working

I've setup a basic auth for quick testing a site.

$headers = array (
    'Authorization' => 'Basic ' . base64_encode( 'admin' . ':' . 'Str0nGP@$$w0rd' ),
);
$url = rest_url( 'wp/v2/posts/1' );

$data = array(
    'title' => 'Hello Gaia'
);

$response = wp_remote_post( $url, array (
    'method'  => 'POST',
    'headers' => $headers,
    'body'    =>  $data
) );

var_dump($response['body']);

Pretty much the example the official docs provide. But I get an error all the time.

{"code":"rest_cannot_edit","message":"Sorry, you are not allowed to edit this post.","data":{"status":401}}

So I tried a different site (site with minimal plugins, twenty*theme) on a different server (apache and nginx).
Same thing.

Am I doing something wrong? Is basic auth still supported or outdated?
I know there are other ways to authenticate.
I'm not asking about that.

I want too know why basic auth doesn't work.

Answers 1

  • The REST API does not support basic authentication out of the box, you need a plugin. The documentation points to this one, accompanied by this warning:

    Note that this plugin requires sending your username and password with every request, and should only be used for development and testing i.e. not in a production environment.


Related Questions