RESTful
OpenKM has a complete API exposed via REST. You can call any API method from any programming language, like Java, PHP, or Python. This feature makes it possible to create a custom client or integrate with third-party applications like a CRM or a CMS.
In most samples, you’ll see parameters named “docId”, “fldId”, or “nodeId”. The value of these parameters must be a valid node UUID.
Sample usage
Section titled “Sample usage”You can use an HTTP client library or any REST client to try these API methods, which makes the process easier. Or you can use the curl command-line application. For example, you can list a folder’s children:
$ curl -u okmAdmin:admin -H "Accept: application/json" \ http://localhost:8080/openkm/rest/folders/de7b07c2-e707-4390-b5cc-cae340c8c3e5/childrenThe result is:
[ { "@type": "folder", "created": 1568408949000, "lastModified": 1568409037000, "path": "/okm:root/test/test", "author": "okmAdmin", "permissions": 31, "uuid": "0fd9f008-27e8-4980-a22b-d71c9a34060a", "parent": "de7b07c2-e707-4390-b5cc-cae340c8c3e5", "subscribed": false, "nodeClass": 0, "dispositionCurrentStage": 0, "subscriptors": [], "keywords": [], "categories": [], "notes": [], "linkTarget": null, "actualVersion": { "name": "1.1", "created": 1568409037000, "size": 0, "author": "okmAdmin", "actual": true, "comment": null, "checksum": null, "action": "PROPERTY_GROUP_ADDED", "params": "okg:consulting", "uuid": null, "parent": "0fd9f008-27e8-4980-a22b-d71c9a34060a" }, "promotedAsRecord": false, "hasRelations": false, "description": null, "restricted": false, "nodeClassChildren": [], "metadataLabel": null, "hasChildren": false, "style": 0 }]In this case, you can see the result in JSON format.
This is a Java client for the same call:
package com.openkm;
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 = "de7b07c2-e707-4390-b5cc-cae340c8c3e5"; URL url = new URL("http://localhost:8080/openkm/rest/folders/" + fldUuid + "/children"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json");
Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("okmAdmin", "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 samples
Section titled “Folder samples”Create a new folder:
$ curl -u okmAdmin:admin -H "Accept: application/json" \ -X POST -H "Content-Type: application/json" -d 'newfolder' \ http://localhost:8080/openkm/rest/folders/parents/fe239ae2-4d25-4d87-88b2-924d03b87faaRename a folder:
$ curl -u okmAdmin:admin -H "Accept: application/json" \ -X PUT -H "Content-Type: application/x-www-form-urlencoded" \ -d 'newName=test2' \ http://localhost:8080/openkm/rest/folders/fe239ae2-4d25-4d87-88b2-924d03b87faa/nameDocument samples
Section titled “Document samples”To create a document, we need to provide the document binary data:
$ curl -u okmAdmin:admin -H "Accept: application/json" \ -X POST -F content=newDoc.txt -F content=@/home/openkm/doc1.txt \ http://localhost:8080/openkm/rest/documents/parents/fe239ae2-4d25-4d87-88b2-924d03b87faaOr from an HTML form:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document Upload</title></head><body> <form method="POST" enctype="multipart/form-data" action="http://localhost:8080/openkm/rest/documents/parents/fe239ae2-4d25-4d87-88b2-924d03b87faa"> Select file: <input type="file" name="content" size="45"/><br/> <input type="submit" value="Upload" /> </form></body></html>