cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a root group using the REST API ?

ferbru60
Champ in-the-making
Champ in-the-making
I try to create (using PERL) a root group using the REST API. according to lot of mix of the docs http://localhost:8080/alfresco/api/index/all and http://wiki.alfresco.com/wiki/3.2_REST_API_Group_Service I coded that :
use warnings;
use strict;

use HTTP::Request::Common;
use HTTP::Request;
use HTTP::Response;
use HTTP::Headers;
use LWP::UserAgent;

use JSON;

my $alfTicket;

my $host = 'localhost';
my $port = 8080;

sub login ()
{
   my ($username, $password) = @_;

   my $ua = LWP::UserAgent->new;
   my $h = HTTP::Headers->new;
   $h->header ('Content-Type' => 'text/x-json', Accept=>'text/x-json');
   my $GetReq = HTTP::Request->new
      (
         'POST',
         "http://$host:$port/alfresco/service/api/login",
         $h,
         '{"username" : "' . $username . '","password" : "' . $password . '"}'
      );
   my $res = $ua->request($GetReq);
    # Check the outcome of the response
    if ($res->is_success)
    {
      my $hashRef = JSON::decode_json ($res->content);
      $alfTicket = $hashRef->{"data"}->{"ticket"};
        return 1;
    }
    else
    {
      print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
      print "\t" . $res->content . "\n";
        return 0;
    }
}

#—————————————————————————
sub createGroup ()
{
   my ($shortName, $groupInfosRef) = @_;

   my $content = JSON::encode_json ($groupInfosRef);
   my $ua = LWP::UserAgent->new;
   my $h = HTTP::Headers->new;
   $h->header ('Content-Type' => 'text/x-json', Accept=>'text/x-json');
   my $GetReq = HTTP::Request->new
      (
         'POST',
         "http://$host:$port/alfresco/service/api/rootgroups/$shortName?alf_ticket=$alfTicket",
         $h,
         $content
      );
   print "—————————-\n";
   print "Request :\n" . $GetReq->as_string . "\n";
   print "—————————-\n";
   my $res = $ua->request($GetReq);
    if ($res->is_success)
    {
      print "—————————-\n";
      print "Response :\n";
      print $res->content;
      print "\n";
        return 1;
    }
    else
    {
      print "—————————-\n";
      print "Response :\n";
      print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
      print "\t" . $res->content . "\n";
      print "—————————-\n";
        return 0;
    }
}

print "admin: ";
my $admin = <STDIN>;
chomp ($admin);

print "admin password: ";
my $password = <STDIN>;
chomp ($password);

if (&login ($admin, $password))
{
   print "$admin is logged on !\n";
   print "Group creation :\n";
   print "Enter shortName : ";
   my $shortName = <STDIN>;
   chomp ($shortName);
   print "Enter fullName : ";
   
   my %groupInfos;
   $groupInfos {"shortName"} = $shortName;
   $groupInfos {"authority"} = 'Group';

   my @groups;
   push (@groups, \%groupInfos );
   if (&createGroup ($shortName, \%groupInfos))
   {
      print "$shortName created !\n";
   }
   else
   {
      print "Can't create $shortName\n";
   }
}
else
{
   print "$admin can't log in $host:$port\n";
}

And I obtain :
—————————-
Request :
POST http://localhost:8080/alfresco/service/api/rootgroups/students?alf_ticket=TICKET_97db1c3bccfddb0eb3f...
Accept: text/x-json
Content-Type: text/x-json

{"authority":"Group","shortName":"students"}

—————————-
—————————-
Response :
Error (500) : Erreur Interne de Servlet
   {
    "status" :
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  }, 
 
  "message" : "09040012 Wrapped Exception (with status template): 09040011 Error during processing of the template 'Error executing macro: authorityJSON\nrequired parameter: authority is not specified.'. Please contact your system administrator.", 
  "exception" : "org.alfresco.web.scripts.WebScriptException - 09040012 Wrapped Exception (with status template): 09040011 Error during processing of the template 'Error executing macro: authorityJSON\nrequired parameter: authority is not specified.'. Please contact your system administrator.",
 
  "callstack" :
  [
       ""      ,"freemarker.template.TemplateException: Error executing macro: authorityJSON\nrequired parameter: authority is not specified."
      ,"freemarker.core.Macro$Context.sanityCheck(Macro.java:181)"
      ,"freemarker.core.Macro$Context.runMacro(Macro.java:161)"
      ,"freemarker.core.Environment.visit(Environment.java:601)"
      ,"freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.MixedContent.accept(MixedContent.java:92)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.Environment.process(Environment.java:188)"
      ,"freemarker.template.Template.process(Template.java:237)"
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:202)"
      ,"org.alfresco.web.scripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:523)"
      ,"org.alfresco.web.scripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:241)"
      ,"org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:357)"
      ,"org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:326)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:407)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:424)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:288)"
      ,"org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:262)"
      ,"org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:139)"
      ,"org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:122)"
      ,"javax.servlet.http.HttpServlet.service(HttpServlet.java:717)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)"
      ,"org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)"
      ,"org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)"
      ,"org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)"
      ,"org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)"
      ,"org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)"
      ,"org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)"
      ,"org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)"
      ,"java.lang.Thread.run(Thread.java:619)"
      ,"org.alfresco.service.cmr.repository.TemplateException: 09040011 Error during processing of the template 'Error executing macro: authorityJSON\nrequired parameter: authority is not specified.'. Please contact your system administrator."
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:206)"
      ,"org.alfresco.web.scripts.WebScriptException: 09040012 Wrapped Exception (with status template): 09040011 Error during processing of the template 'Error executing macro: authorityJSON\nrequired parameter: authority is not specified.'. Please contact your system administrator."
      ,"org.alfresco.web.scripts.AbstractWebScript.createStatusException(AbstractWebScript.java:613)"

  ],
 
  "server" : "Alfresco Community v3.2.0 (2039) schema 2 019",
  "time" : "4 oct. 2009 21:24:18"
}

According to the docs, I've tried :

$groupInfos {"authority"} = 'Group'

Then
$groupInfos {"authority"} = 'GROUP'

Then
$groupInfos {"authorityType"} = 'Group';

An finally :
$groupInfos {"authorityType"} = 'GROUP'

And I've allways got the same error !

Why ? How can I create a root group with this API ?

Bruno
2 REPLIES 2

mikeh
Star Contributor
Star Contributor
For both your questions, you need Content-Type to be "application/json", not "text/x-json"

Thanks,
Mike

ferbru60
Champ in-the-making
Champ in-the-making
Thanks ! It's OK now ! I can create root groups !

Bruno