PHP REST API got error when try to add New Users to Jasperserver

1k Views Asked by At

I got some problem with php-sample. I want to add users, but there were something went wrong. Please help me!! Here is code that I used:

<!DOCTYPE html>
<?php
    require_once "vendor/autoload.php";
    require_once "autoload.dist.php";
    require_once "client/JasperClient.php";
    require_once "client/User.php";
    require_once "client/Role.php";
    $client = new Jasper\JasperClient(
                    "localhost", // Hostname
                    8080, // Port
                    "jasperadmin", // Username
                    "jasperadmin", // Password
                    "/jasperserver-pro", // Base URL
                    "organization_1"
                ); // Organization (pro only)

    $newUser = new Jasper\User("BI_User",                // username
           "superSTRENGTHpassw0rd",      // password
           "[email protected]",           // email
           "Business Intelligence User", // description
           "organization_1",             // parent organization
           "true"                        // enabled
           );

   $role = new Jasper\Role("ROLE_USER", NULL, "false");
   $newUser->addRole($role);

   try {
      $client->putUsers($newUser);
  }
  catch (Exception $e) {
      printf("Could not add new user: %s", $e->getMessage());
  }?>

And Here is the error message that I got:

Could not add new user: Unexpected HTTP code returned: 400 Body of response: 
Apache Tomcat/6.0.26 - Error report HTTP Status 400 - type Status 
reportmessage description The request sent by the client was syntactically 
incorrect ().Apache Tomcat/6.0.26
1

There are 1 best solutions below

0
On

Ask I spent time google so much on that problem, I have found the solution. Here is my solution whether someone are interested.

<?php
require_once "vendor/autoload.php";
require_once "autoload.dist.php";

use Jaspersoft\Client\Client;
use Jaspersoft\Dto\User\User;
use Jaspersoft\Dto\Role\Role;


function registerUsers(){
    $client = new Client(
            "localhost",
            "8080",
            "superuser",
            "superuser",
            "/jasperserver-pro",
            "null"
       );
    $file_path = 'data/userlist.csv';
    $handle = fopen($file_path,'r');
    $array_users = array();
    $i=0;
    while (!feof($handle) !==false){
        $line = fgetcsv($handle,1024,',');
        $i++;
        if($i==1) continue;
        if(!empty($line)){
            for($c = 0; $c < count($line); $c++){
                $username = $line[0];
                $password = $line[1];
                $email = $line[2];
                $fullname = $line[3];
                $tenantId = $line[4];
                $enabled = $line[5];
                $user = new User($username, $password, $email, 
                                $fullname, $tenantId, $enabled);
                $role = new Role('ROLE_USER', null, 'false');
                $array_users[$c] = $user;
                $array_users[$c]->addRole($role);
                try {
                    $client->userService()->addUsers($array_users[$c]);
                } catch (Exception $e) {
                    printf('Could not add new user: %s', $e->getMessage());
                }
            }
        }
    }
}?>

And here is my csv data file:

User Name,Password,Email Address,Full Name,Tenant ID,Enable
User1,superSTRENGTHpassw0rd,[email protected],User One,a,true
User2,superSTRENGTHpassw0rd,[email protected],User One,a,true
User3,superSTRENGTHpassw0rd,[email protected],User One,a,true
User6,superSTRENGTHpassw0rd,[email protected],User One,organization_1,true
User7,superSTRENGTHpassw0rd,[email protected],User One,organization_1,true
User8,superSTRENGTHpassw0rd,[email protected],User One,b,true
User9,superSTRENGTHpassw0rd,[email protected],User One,b,true
User10,superSTRENGTHpassw0rd,[email protected],User One,c,true
User11,superSTRENGTHpassw0rd,[email protected],User One,c,true