RESTful Guide with curl

From LogicalDOC Community Wiki
Revision as of 10:18, 28 July 2016 by Blucecio (talk | contribs) (Sample usage)
Jump to navigationJump to search


Classic note.png If you're interested in use LogicalDOC API we suggest to take a look at ours Bindings and Samples and take advantage of already usable examples which connect to webservices API.


LogicalDOC has a complete API exposed via REST. This means you can call any of these API methods from any programming language, like Java, PHP or Python among others. This feature makes it possible to create a custom client, or integrate with third-party applications like a CRM or a CMS.


Note idea.png This API is only available since LogicalDOC 7.5 and is currently in development, so expect changes and additions.


If you point your browser to http://localhost:8080/logicaldoc/services, you can see the SOAP API at first place but at the bottom you will see a Available RESTful services section. These URLs are protected by BASIC authentication so you need to provide an user and password to access them.

Sample usage

To try these API methods you can use an HTTP Client library or any REST client which ease this process. Or simply you can use the curl command-line application. For example, you can list the children folders:

 $ curl -u admin:admin -H "Accept: application/json" \
   http://localhost:8080/logicaldoc/services/rest/folder/listChildren?folderId=4

The result is:

[
  {
    "id": 3440640,
    "name": "alfa",
    "parentId": 4,
    "description": "",
    "lastModified": "2016-06-15 15:49:40 +0200",
    "type": 0,
    "templateId": null,
    "templateLocked": 0,
    "creation": "2016-06-15 15:49:40 +0200",
    "creator": "Admin Admin",
    "position": 1,
    "hidden": 0,
    "foldRef": null,
    "attributes": [
      
    ]
  },
  {
    "id": 3440643,
    "name": "beta",
    "parentId": 4,
    "description": "",
    "lastModified": "2016-06-16 10:16:25 +0200",
    "type": 0,
    "templateId": null,
    "templateLocked": 0,
    "creation": "2016-06-16 09:49:27 +0200",
    "creator": "Admin Admin",
    "position": 1,
    "hidden": 0,
    "foldRef": null,
    "attributes": [
      
    ]
  }
]

In this case you can see the result in JSON format. Otherwise you can need an XML output, which can be forced using the 'Accept header:

 $ curl -u admin:admin -H "Accept: application/xml" \
   http://localhost:8080/logicaldoc/services/rest/folder/getChildren?fldId=3492d662-b58e-417c-85b6-930ad0c6c3cf

The result in XML is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<folders>
  <folder>
    <author>admin</author>
    <created>2013-07-29T12:09:11+02:00</created>
    <path>/okm:root/alfa</path>
    <permissions>15</permissions>
    <subscribed>false</subscribed>
    <uuid>6b3e0531-96a9-4675-bb82-215b715b20ca</uuid>
    <hasChildren>false</hasChildren>
  </folder>
  <folder>
    <author>admin</author>
    <created>2013-07-24T22:56:20+02:00</created>
    <path>/okm:root/beta</path>
    <permissions>15</permissions>
    <subscribed>false</subscribed>
    <uuid>41f1bace-58b4-41a9-b43e-dffc1ac9a954</uuid>
    <hasChildren>false</hasChildren>
  </folder>
</folders>

This is a Java client for the same call:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;

public class JavaRestClient {
    public static void main(String[] args) throws Exception {
        try {
            String fldUuid = "3492d662-b58e-417c-85b6-930ad0c6c3cf";
            URL url = new URL("http://localhost:8080/logicaldoc/services/rest/folder/getChildren?fldId=" + fldUuid);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            
            Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("admin", "admin".toCharArray());
                }
            });
            
            if (conn.getResponseCode() == 200) {
                BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
                System.out.println("Output from Server .... \n");
                String output;
                
                while ((output = br.readLine()) != null) {
                    System.out.println(output);
                }
            } else {
                System.err.println("Failed : HTTP error code : " + conn.getResponseCode());
            }
            
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Folder

Let's create a new folder:

 $ curl -u admin:admin -H "Accept: application/json" \
   -X POST -H "Content-Type: application/json" -d '/okm:root/newfolder' \
   http://localhost:8080/logicaldoc/services/rest/folder/createSimple

Document

Now we are going to create a document. For this, we need to provide the document binary data:

 $ curl -u admin:admin -H "Accept: application/json" \
   -X POST -F docPath=/okm:root/newDoc.txt -F content=@newDoc.txt \
   http://localhost:8080/logicaldoc/services/rest/document/createSimple

Or also from a HTML form:

<html>
  <body>
    <form method="POST" enctype="multipart/form-data"
          action="http://localhost:8080/logicaldoc/services/rest/document/createSimple">
      Select file: <input type="file" name="content" size="45"/><br/>
      Select path: <input type="text" name="docPath" value="/okm:root/newDoc.txt"/><br/>
      <input type="submit" value="Upload" />
    </form>
  </body>
</html>

And now download it:

 $ curl -u admin:admin \
   http://localhost:8080/logicaldoc/services/rest/document/getContent?docId=58f79fa6-fe6e-4f68-8517-68a60898d122

Search

Search simply by content:

 $ curl -u admin:admin -H "Accept: application/json" -X GET \
   http://localhost:8080/logicaldoc/services/rest/search/findByContent?content=santo+grial

Search by keyword:

 $ curl -u admin:admin -H "Accept: application/json" -X GET \
   http://localhost:8080/logicaldoc/services/rest/search/findByKeywords?keyword=santo\&keyword=grial

Or use a more customized search parameters:

 $ curl -u admin:admin -H "Accept: application/json" -X GET \
   http://localhost:8080/logicaldoc/services/rest/search/find?content=grial\&mimeType=application/pdf

Even can query by Property Groups:

 $ curl -u admin:admin -H "Accept: application/json" -X GET \
   http://localhost:8080/logicaldoc/services/rest/search/find?content=grial\&property='okp:name=alfa'


Notes

To create a note in a LogicalDOC folder or document you can do this (previously you need to know the node UUID):

 $ curl -u admin:admin -H "Accept: application/json" \
   -X POST -H "Content-Type: application/json" -d 'Hello, world!' \
   http://localhost:8080/logicaldoc/services/rest/note/add?nodeId=3492d662-b58e-417c-85b6-930ad0c6c3cf


More info at: