AEM with cURL


Hi,

Through this post , I would like to introduce cURL commands for numerous applications in AEM. Its quite a handy tool to deal with packages, backups, and various usecases in AEM. You may find ample of applications of cURL by the end of this post. After going through the post you will be able to write curl commands for almost any functionality in AEM on your own.

So lets get started.

What is cURL?

cURL is a command line tool for doing all sorts of URL manipulations and transfers. It is used for transferring data using various protocols, HTTP,FTP, Gopher, TELNET, etc. It comes in handy for automation, and if you know how script you can create powerful tools that will facilitate your work process. The name stands for Curl URL Request Library.

Installation

You may download and install cURL  from http://curl.haxx.se/download.html as per the OS you are using . For Windows users make sure you have appended the Environment variable ‘Path‘ with C:\Program Files\cURL\bin (or the path of bin where you have installed cURL).

Now you may able to open your command prompt and type curl. If everything is fine your cURL is installed. Go try it using

cURL help:

curl --help

Install the manual for all the default cURL commands: This will help you as a reference if you want to explore more in cURL


curl --manual > <curlManual.txt>
/*give complete path of the text file curlManual.txt above */

cURL GET:

curl www.google.co.in

Useful cURL Commands 

Now let us jump to AEM application of cURL . cURL is mostly used for handling packages in AEM , but you may use it for other purposes too. As AEM is based on REST protocol , cURL can be used to do anything and everything in AEM . I will start stating its application with commands and examples : starting from simpler to complex usage.

You may visit your AEM instance and hit the URL http://localhost:4502/crx/packmgr/service.jsp for getting the list of commands which are useful in AEM. I will illustrate each application below. All the below commands are tried and tested on AEM 6.0

Package Management Commands: 

  • Help Menu for useful AEM commands
curl -u admin:admin http://localhost:4502/crx/packmgr/service.jsp?cmd=help
  • List of all the packages in your AEM instance
curl -u admin:admin http://localhost:4502/crx/packmgr/service.jsp?cmd=ls
  •  Build an existing package
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/samplepackage.zip?cmd=build
  •  Delete an existing package
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/samplepackage.zip?cmd=delete
  •  Install an existing package
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/samplepackage.zip?cmd=install
  •  Uninstall an existing package
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/samplepackage.zip?cmd=uninstall
  •  Download an existing package into filesystem
curl -u admin:admin http://localhost:4502/etc/packages/my_packages/samplepackage.zip > <local filepath>
  •  Upload and don’t install an existing package from File system
curl -u admin:admin -F file=@"C:\sample\samplepackage.zip" -F name="samplepackage" -F force=true -F install=false http://localhost:4502/crx/packmgr/service.jsp
  •  Upload and Install an existing package from File system
curl -u admin:admin -F file=@"C:\sample\samplepackage.zip" -F name="samplepackage" -F force=true -F install=true http://localhost:4502/crx/packmgr/service.jsp

User Management Commands:

  • Create a new User
curl -u admin:admin -FcreateUser= -FauthorizableId=hashim -Frep:password=hashim http://localhost:4502/libs/granite/security/post/authorizables
  • Create a new Group
curl -u admin:admin -FcreateGroup=group1 -FauthorizableId=testGroup1 http://localhost:4502/libs/granite/security/post/authorizables
  • Add a Property to an existing User
curl -u admin:admin -Fprofile/age=25 http://localhost:4502/home/users/h/hashim.rw.html
  • Create a User with a profile
curl -u admin:admin -FcreateUser=testuser -FauthorizableId=hashimkhan -Frep:password=hashimkhan -Fprofile/gender=male http://localhost:4502/libs/granite/security/post/authorizables
  • Create a new User as a member of a Group
curl -u admin:admin -FcreateUser=testuser -FauthorizableId=testuser -Frep:password=abc123 -Fmembership=contributor http://localhost:4502/libs/granite/security/post/authorizables
  • Add User to a Group
curl -u admin:admin -FaddMembers=testuser1 http://localhost:4502/home/groups/t/testGroup.rw.html
  • Remove a User from a Group

curl -u admin:admin -FremoveMembers=testuser1 http://localhost:4502/home/groups/t/testGroup.rw.html
  • Set a User’s Group Memberships
curl -u admin:admin -Fmembership=contributor -Fmembership=testgroup http://localhost:4502/home/users/t/testuser.rw.html
  • Delete user and Group

curl -u admin:admin -FdeleteAuthorizable= http://localhost:4502/home/users/t/testuser
curl -u admin:admin -FdeleteAuthorizable= http://localhost:4502/home/groups/t/testGroup
  • Change a user password
curl -u testuser:OLD_PWD -F rep:password=”NEW_PWD” http://localhost:4502/home/users/t/testuser.rw.html
curl rep:password=”test” –user admin:admin http://localhost:4502/home/users/a/alister@geometrixx.com

JCR Node Management Commands:

  • Delete a Node
curl -X DELETE http://localhost:4502/content/geometrixx/en/products/jcr:content/par/flash -u admin:admin
  • Create or Add a Node
curl --data jcr:primaryType=nt:unstructured --user admin:admin http://localhost:4502/content/geometrixx/en/toolbar/test3
  • Create a Page (Although its not recommended to do with cURL, as we have Page Manager API for this.)
curl -u admin:admin -F "jcr:primaryType=cq:Page" -F "jcr:content/jcr:primaryType=cq:PageContent" -F "jcr:content/jcr:title=Curl Page" -F "jcr:content/sling:resourceType=geometrixx/components/contentpage" http://localhost:4502/content/geometrixx/en/curlPage

AEM Replication Commands:

  • Activate
curl -u admin:admin -X POST -F path="/content/geometrixx/en/pag" -F cmd="activate" http://localhost:4502/bin/replicate.json
  • Deactivate
curl -u admin:admin -X POST -F path="/content/geometrixx/en/pag" -F cmd="deactivate" http://localhost:4502/bin/replicate.json
  • Tree Activation
curl -u admin:admin -F cmd=activate -F ignoredeactivated=true -F onlymodified=true -F path=/content/geometrixx/en/community http://localhost:4502/etc/replication/treeactivation.html

OSGi Bundle Management Commands:

  • Build a Bundle
curl -u admin:admin -F bundleHome=/apps/training/src/com.day.sample -F descriptor=/apps/training/src/com.day.sample/com.day.sample.bnd http://localhost:4502/libs/crxde/build
  • Start a Bundle
curl -u admin:admin http://localhost:4502/system/console/bundles/com.day.sample -Faction=start
  • Stop a Bundle
curl -u admin:admin http://localhost:4502/system/console/bundles/com.day.sample -Faction=stop
  • Install a Bundle from File system
curl -u admin:admin -F action=install -F bundlestartlevel=20 -F bundlefile=@”<path of samplejar.jar>” http://localhost:4502/system/console/bundles

Page Management Commands:

  • Lock a Page
curl -u admin:admin -X POST -F cmd="lockPage" -F path="/content/geometrixx/en/toolbar/contacts" -F "_charset_"="utf-8" http://localhost:4502/bin/wcmcommand
  • Unlock a Page
curl -u admin:admin -X POST -F cmd="unlockPage" -F path="/content/geometrixx/en/toolbar/contacts" -F "_charset_"="utf-8" http://localhost:4502/bin/wcmcommand
  • Copy/Move a Page
curl -u admin:admin -F:operation=copy -F:dest=/content/geometrixx/en/products/contacts http://localhost:4502/content/geometrixx/en/toolbar/contacts

JCR Query API Commands:

  • Find an Asset from the JCR
curl -s -u admin:admin GET "http://localhost:4502/bin/querybuilder.json?path=%2fcontent%2fgeometrixx%2fen&property=fileReference&property.value=%2fcontent%2fdam%2fgeometrixx%2fshapes%2ftri_equilateral.png&type=nt%3aunstructured"

Alternatively, you may use Query Debugger to initiate any query you want and copy the “JSON QueryBuilder Link” given there to initiate the same in cURL.


Backup Commands:

  • Initiate a Backup to a Target folder
curl -u admin:admin -X POST http://localhost:4502/system/console/jmx/com.adobe.granite%3Atype%3DRepository/op/startBackup/java.lang.String?target=C:\sample\backupTest.zip
  • Stop a running Backup
curl -u admin:admin -X POST http://localhost:4502/libs/granite/backup/content/admin/backups.cancel.html

Alternatively , you may use AEM Backup Console for initiating a backup for your AEM instance.


How to use cURL for any functionality in AEM:

As you all might be knowing that AEM is completely based on REST , so you can easily trace any POST/GET call and write a cURL for that call. Its quite simple and handy to do so with the following steps:

  • As an example , I have picked Backup for AEM instance. Open to the URL in Firefox.
  • Open the Firebug console and go to the Network Tab.
  • Fill the form for initiating the Backup as shown below

22

  • While clicking on Start keep a watch at the POST call from the Firebug Console>Network.

11

  • In every functionality you will see a call to server: either a POST or GET. Here you will find a POST call to the
  • After observing these values you can write your cURL command as:
curl -d "_charset_=utf-8&force=false&installDir=C%3A%5CAEM+Dev%5CAEM&target=C%3A%5Csample%5CBackup&delay=10" -u admin:admin -X POST http://localhost:4502/libs/granite/backup/content/admin/backups/

This way you will be able to convert any of the AEM functionality in cURL. You can try exploring this from the System Console while modifying any configurations.

I hope these commands will help you understand and use cURL in a better way. Do let me know if there are any problems you are facing with cURL , I will try to help you guys.

Go Explore cURL !!

Advertisements

34 thoughts on “AEM with cURL

  1. Pingback: Automate your cURL Completely – CQ5 AEM Tricks of Trade

    • Hi,
      You can find out the curl for any of the functionaliity from the browser console call. Its very simple. Just perform the event which you want to do and make a similar call using curl. e.g. –

      curl -u admin:admin -X POST -F cmd=”createVersion” -F path=”/content/geometrixx/en/toolbar/contacts” -F “_charset_”=”utf-8” http://localhost:4502/bin/wcmcommand

      Hope it helps

      Regards
      Hashim

      Like

  2. Hi Hashim, I am trying to hit a Servlet or Service deployed in OSGi bundle using cURL. I used something like this:

    curl -u admin:admin -F cmd=”generateMyPage” -F label=”” -F parentPath=”/content/brand/en/homepage” -F template=”/apps/brand/templates/homepage” -F title=”My_New_Page” http://localhost:4502/bin/wcmcommand

    generateMyPage() is a method in my service class which has the code of creating a page. How do I hit this method using a command line cURL ?

    Thanks !

    Like

  3. Hi Hashim,

    I wish to hit a REST servlet (path = /bin/createcontent) or service in AEM using a curl command line. What is the command of doing so ? For service it will be a method to be invoked such as createPages() and for servlet it can be a GET. If I use POST, it says wcmcommand does not support POST.

    Cheers,
    Som

    Like

    • Hi,
      GET command would be a normal cURL command. To invoke a particular method of your servlet in POST you need to pass some parameter in your cURL which will invoke that method in your servlet. You might have to change the parameter usage in the servlet.

      eg. curl -u admin:admin -X POST -F cmd=”unlockPage” -F path=”/content/geometrixx/en/toolbar/contacts” -F “_charset_”=”utf-8” http://localhost:4502/bin/wcmcommand

      In the above command, the cmd=”unlockPage” is the parameter whose value decides which method to call in the servlet /bin/wcmcommand

      I hope this will help you.

      Thanks
      Hashim

      Like

  4. Hi Hashim,

    I was wondering if it’s possible to add a property to every single page returned from the querybuilder that matches a particular case? For example, I want to add a property to every page that is of sling:resourceType – foundation/components/redirect.

    Below is the curl command to get all pages of the desired type in the path. I’ve changed one of the page types to verify this command works correctly.

    curl -s -u admin:admin -X GET “http://localhost:4502/bin/querybuilder.json?path=/content/geometrixx/en/company&type=cq:Page&p.hits=selective&p.properties=jcr:content/sling:resourceType&property=jcr:content/sling:resourceType&property.1_value=foundation/components/redirect&p.limit=-1”

    Like

    • Hi,
      In the blog above I have shared how to build a cURL command for any action in AEM . Please read that . Try something similar to

      curl -d “_charset_=utf-8&payloadType=JCR_PATH&:status=browser&payload:/content/geometrixx/pagepath&model=/etc/workflow/models/request_for_activation/jcr:content/model&startComment=Comment&workflowTitle=Title” -u admin:admin -X POST http://localhost:4502/etc/workflow/instances

      Like

  5. Hi Hashim,

    I am trying to download an AEM package to my local file system using curl in java. I am using the command as:

    ProcessBuilder packageDownloadProcess = new ProcessBuilder(“curl”, “-u”, “admin:admin”, “”, “”, curlDownloadPath, “>”, fileFullPath);

    Here curlDownloadPath = http://localhost:4502/etc/packages/cox/Application_LOB_PackageName_20160902153139.zip;

    and

    fileFullPath = C:\data\Application_LOB_PackageName_20160902153139;

    On doing so, I am not able to download the package to my local file system. Please help me out.

    Thanks
    Nitul

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s