Wednesday, 31 August 2016

Rest APIs

Hello Friends,

I am back with another stuff from Mark Logic which is known as REST-APIs in Mark Logic. REST-APIs term is used for restful web services. 

RESTful Web Services are actually REST architecture based web services where REST stands for representational state transfer and runs on HTTP protocol. RESTful web services are light weight, highly scalable and maintainable and are commonly used to create API/Web services for web application.

Mark logic also supports rest apis to expose communication with Mark Logic database to web applications. Mark Logic provides many in-build Rest Apis which can be used to achieve common functionality like create, update, delete and search etc. We can use these Mark Logic provided REST apis by creating new instance than we can customize it further as per our requirement but we can say that basic features are ready to use for specific database. 

In terms of Mark Logic we can say that REST APIs are specifically configured http servers that provides access to Database contents for document creation, updation, deletion and searching etc.
Already installed/configured rest-apis can be viewed using below URL


Most frequently used service is /documents which is used for document manipulation including creating, updating, deleting of documents and metadata. 

Now let come to some practical examples. We need curl command tool to call the Mark Logic rest apis for various setup/configuration.

Below is the curl command to create an instance of Rest-Api which includes creation on database, modules and configuration of rest-api to use.

curl --anyauth -u admin:admin -X POST --header "Content-Type:application/json" -d "{\"rest-api\": { \"name\":\"testApi\", \"port\":\"8011\", \"database\":\"testApi-DB\", \"modules-database\":\"testApi-Modules\" } }" http://localhost:8002/LATEST/rest-apis

Above command should create following that you can check in Mark Logic admin panel
  1. A new database as “testApi-DB”
  2. A module database as “testApi-Modules”
  3. A http App server at port 8011 with name as “testApi”

You can verify your rest api using below URL as well.

http://localhost:8002/LATEST/rest-apis

Now once the instance of rest-api is created, it is ready to be used to operate on database to create, update, delete documents  using /documents service of created rest-api.  

Below are example for various uses of documents service  

Get document using URI  
We can get the document contents using it’s URI. 
For example 
If an document exist in testApi-DB with URI as “/content/dbreg/demographics/1002970.xml” Then below URL should give you the contents of document.
http://localhost:8011/LATEST/documents?uri=/content/dbreg/demographics/1002970.xml

Create new document
Below is the curl command to create a new document using rest-api instance with /documents service
curl --anyauth --user admin:admin -X PUT -d "<root><name>John</name><age>31</age></root>" http://localhost:8011/LATEST/documents?uri=/samples/sample1.xml

Update existing document
Below is the curl command to update existing document.
curl --anyauth --user admin:admin -X PUT -d "<root><name>John Smith</name><age>31</age></root>" http://localhost:8011/LATEST/documents?uri=/samples/sample1.xml

Delete existing document
Below is the curl command to delete existing document
curl --anyauth --user admin:admin -X DELETE http://localhost:8011/LATEST/documents?uri=/samples/sample2.xml

So, This is enough for today. Go ahead and explore more for further uses of Mark Logic rest-api services like /search etc. 

Till than keep exploring and keep sharing.

References
 (Refer above URL to find rest-apis provided by Mark Logic)

No comments:

Post a Comment