cancel
Showing results for 
Search instead for 
Did you mean: 

Difficulty understanding Alfresco REST API

anthony_ramesh
Champ in-the-making
Champ in-the-making
Hello,
  I am new to alfresco and I am trying to create a POC to create/upload a document in Alfresco and retrieve them via the REST api from a different server within the same domain.

I am finding it difficult to get it to work. I would like pointers to examples.

I hope the experts can verify my below understanding of the concepts

First I had a rough time understanding what content{property}, {store_type} and {store_id} and {id} meant now I think I have it after using the node browser I understand
property –> use the default
store_type = 'workspace'
store_id = 'SpacesStore'
I am not sure if this configurable, probably with property files.
and the id is the GUID like number I found from the node browser.

I am successful in calling the login, list services etc. but I am not able to use the content APIs

Questions
1. What is the difference between using the
../node/ 
APIs vs
../path/
  apis?
2. Is there an example with source code perhaps of how to use this assets/documents/content etc somewhere?

3. the rest api naming conventions and actual parameters are difficult to understand and not straightforward to find (using the node browser????). What am I missing? I feel like I am facing a
paradigm shift
having skipped a generation of CMS.

Thanks to any good samaritan who will answer this.

Thanks
-Anthony

6 REPLIES 6

abarisone
Star Contributor
Star Contributor
Hi,
you'd better start reading the documentation:
http://docs.alfresco.com/4.1/topic/com.alfresco.enterprise.doc/references/RESTful-intro.html
http://docs.alfresco.com/4.1/topic/com.alfresco.enterprise.doc/tasks/ws-tutorials.html
Also on your running Alfresco installation you can find more info about REST webscripts looking at <a>http:localhost:8080/alfresco/service/index</a>.
This gives you the description of all webscripts available on your system.

Regards,
Andrea

Abarisone,
  Thanks for the pointer.
I am able to use some of the api such as login etc…
but…
I have already looked into and find it inadequate. For example if you look at http://docs.alfresco.com/4.1/index.jsp?topic=%2Fcom.alfresco.enterprise.doc%2Freferences%2FRESTful-i... for the CMIS it gives you just one function = "Head Content". It mentions {property} {store_type}, {store_id} and {id}.. but id does not say what they are and how to get them

For the other link you sent it is pretty much the same. It doesn't really tell me the paradigm to retrieve content etc. It tells me how to build my own web scripts. I am not interested in that I want to use the existing one.

The following is what I can do and have done
1. read the documentation
2. used some of the REST api successfully such as login, list sites etc eg GET /alfresco/service/api/sites/{shortname}
3. Also read api documentation in the wiki http://wiki.alfresco.com/wiki/3.1_REST_API, http://wiki.alfresco.com/wiki/Alfresco_REST_Design_Guidelines, http://wiki.alfresco.com/wiki/HTTP_API, https://wiki.alfresco.com/wiki/Repository_RESTful_API_Reference

But this is where I get lost
for example say I am working on getContent
GET /alfresco/service/api/node/content{property}/{store_type}/{store_id}/{id}?a={attach?}
GET /alfresco/service/api/path/content{property}/{store_type}/{store_id}/{id}?a={attach?}

There is no explanation of the parameters and how to get them.

I tried the following and I got a generic document, which I think maybe for non implemented webscripts?
http://localhost:8090/alfresco/service/api/node/content/workspace/SpacesStore/a615c7ff-cc1a-481d-a9a...

I got the node ID from the /alfresco site and node browser, I would have expected to find it when I create the content.

Thanks again
-Anthony

kaynezhang
World-Class Innovator
World-Class Innovator
service/api/path/content

cm:content


In fact the webscript api url should be like this

GET /alfresco/service/api/node/content{property}/{store_type}/{store_id}/{id}?a={attach?}&streamId={streamId?}
GET /alfresco/service/api/path/content{property}/{store_type}/{store_id}/{nodepath}?a={attach?}&streamId={streamId?}

instead of

GET /alfresco/service/api/node/content{property}/{store_type}/{store_id}/{id}?a={attach?}
GET /alfresco/service/api/path/content{property}/{store_type}/{store_id}/{id}?a={attach?}

That means if the front part of url is
/alfresco/service/api/node
you should provider {id} paramter.if the front part of url is
/alfresco/service/api/path
you should provide the {nodepath} parameter.

for {store_type} you can just use "workspace"
for {store_id} you can just use "SpacesStore"
for {property} you can omit it ,it'will use the default propert name(cm:content),or you can pass a property name like this ";cm:content"
If you are using the id form ,you should pass node id parameter to it .

Following is an example you can refer to

   public String login() {
      String ticket = null;
      HttpClient client = new HttpClient();

      String apiurl = "http://localhost:8080/alfresco/service/api/login";
      PostMethod post = new PostMethod(apiurl);
      try {
         JSONObject login = new JSONObject();
         login.put("username", "admin");
         login.put("password", "admin");

         //System.out.println(login.toString());
         post.setDoAuthentication(true);
         post.setRequestHeader("Content-Type", "application/json");
         post.setRequestEntity(new StringRequestEntity(login.toString(),
               "application/json", "UTF-8"));

         int status = client.executeMethod(post);
         if (status != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + post.getStatusLine());
         }
         String responseData = post.getResponseBodyAsString();
         
         System.out.println(responseData);
         JSONObject response = new JSONObject(responseData);
         ticket = response.getJSONObject("data").getString("ticket");
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         post.releaseConnection();
      }
      return ticket;
   }
   public void getContent(String ticket){
      HttpClient client = new HttpClient();
      String uid = "e7d585dd-16f1-4b31-9cd5-a9f8e2869a17";
      String apiurl = "http://localhost:8080/alfresco/service/api/node/content;cm:content/workspace/SpacesStore/"
            + uid + "?alf_ticket="+ticket;
      GetMethod get = new GetMethod(apiurl);
      try {

         get.setDoAuthentication(true);
         get.setRequestHeader("Content-Type", "application/json");

         int status = client.executeMethod(get);
         if (status != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + get.getStatusLine());
         }
         String resultString = get.getResponseBodyAsString();
         System.out.println(resultString);
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         get.releaseConnection();
      }
   }
   public static void main(String[] args) {
      LoginAddGetContentTest t = new LoginAddGetContentTest();
      String ticket = t.login();
      System.out.println(ticket);
      t.getContent(ticket);
   }

Thanks Kaynezhang,
Makes a lot of sense now.
One more question.
  Where can I find the <cite> nodepath </cite> and the <cite> id </cite> of the piece of content (node) I need?. There is something available in the /alfresco admin, but is that accessible to the developers?

anthony_ramesh
Champ in-the-making
Champ in-the-making
I really appreciate the response

So, is there documentation that can tell me what these really mean and where I can get it from

{store_id} ==> "Is the ID of ….} and is availabale from ==>

Thanks
anthony

mrogers
Star Contributor
Star Contributor
You should probably be using the Chemistry CMIS library in the first instance that will hide these details from you.   Especially if you just want CRUD.     I don't think there's much documentation for store_id     its simply the id of the store.   As the poster points out you probably want to use "SpacesStore".     You can also poke around in one of the alfresco node browsers to see how alfresco is laid out.