skip to Main Content

We have tried to create a post in wordpress using postman, cant able to create one since we have received error as you are not allowed to do this action

Eventhough i entered the admin login, it gives the same error !!!!

This is the exact error message ,

{
    "code": "rest_cannot_create",
    "message": "Sorry, you are not allowed to create posts as this user.",
    "data": {
        "status": 401
    }
}

I have tried using this URL in the postman (post) method http://localhost/wordpress/wp-json/wp/v2/posts

And in the authorization entered the admin credentials of my application.

Any alternative solutions are also appreciable …..Thank You!!!

2

Answers


  1. you can use this code

    $username = 'yourwordpress login username';
    $password = 'your wordpress login password';
    $rest_api_url = "https://example.com/wp-json/wp/v2/posts";
    
    $data_string = json_encode([
        'title'    => 'your post title',
        'content'  =>  <<<HEREDOC
          
        Add your post content here
    
    HEREDOC
    ,
        'status'   => 'publish',
    ]);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $rest_api_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string),
        'Authorization: Basic ' . base64_encode($username . ':' . $password),
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec($ch);
    
    curl_close($ch);
    
    if ($result) {
       echo 'yes';
    } else {
      echo 'no';
        // ...
    }
    
    Login or Signup to reply.
  2. There is a plugin available in WordPress for Basic Authentication configuration, that will help. It’s free for Basic Authentication you can search the plugin name REST API Authentication in your site and configure the Basic Authentication setup. And you can create the post using the WordPress endpoints.
    https://developer.wordpress.org/rest-api/reference/posts/

    Note : It will be safer to create a new user by having Author role. So that use can access the site only for creating the posts.
    Here I’m adding the plugin documentation for reference
    https://plugins.miniorange.com/wordpress-rest-api-basic-authentication-method

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search