Adobe CQ5 AEM cURL Commands

The focus of this tutorial is on basic or most frequently used AEM cURL commands. There are no limit of number of cURL commands that you can use with AEM .In this tutorial, I will show you how to create any custom cURL command and few flavours of basic AEM cURL commands.

After completing this article you will have a clear understanding of :-

What is cUrl ?

cURL is an open source command line tool for performing URL Manipulations . It is designed for transferring data  using various protocols like  FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP and many more. cURL was released in 1997 . The name cURL stands for  “see URL“.

How to install cUrl ?

You can download cURL executable file from http://curl.haxx.se/dlwiz/ for your operating system or cURL  from http://curl.haxx.se/download.html according to your operating system. Windows users can follow below steps if you have downloaded executable file :-

  • Unzip the tar file.
  • Set the Environmental Variable. Append the path of cURL.exe directory(D:\cURL\AMD64\) or custom path where you have unzipped the file to “Path variable”.
  • Open command prompt type curl –help and enter. If everything is ok u will get Curl basic commands syntaxes else you will get an error message “curl” is not recognized as internal or external batch command.

How to find cURL command for any AEM Operation?

In this article i have covered most frequently asked AEM cURL commands . But you are free to find out any cURL command used for any AEM operation. Follow below steps to know any AEM cURL command :-

  • Use FireFox –> Press F12 and open FireBug console.
  • Click on “NET” tab in FireBug and enable it by clicking “Enable”.
  • Perform any AEM operation on webpage.
  • Click “invoke”.
  • In FireBug console , you will see the full request URL when you take mouse on top of your request url under POST tab . Copy full path along with parameters from “POST” tab under it.
  • You got your URL that you can append at the end of cURL command and access the same resource using cURL.

Above process is very much useful for testers to automate AEM operations. Lets have a look at AEM cURL commands.

List of useful AEM cURL commands and its usage

As we all know AEM is based on REST protocol, it supports only http request and cURL can be used as a magical wand to do anything in AEM in just a blink.

I have divided cURL commands based on its usage into following sections :-

AEM User Management cURL commands :

Below are the AEM cUrl commands for User Management : –

  • Create a new User using cURL AEM command
curl -u admin:admin -FcreateUser= -FauthorizableId=ankur-Frep:password=ankur http://localhost:4502/libs/granite/security/post/authorizables
  • Create a new user under specified folder
curl -u admin:admin -FcreateUser= -FauthorizableId=testuser -Frep:password=testuser -FintermediatePath="/home/users/test/" http://localhost:4502/libs/granite/security/post/authorizables
  • Create a new Group using cURL AEM command
curl -u admin:admin -FcreateGroup=testgroup -FauthorizableId=testGroup http://localhost:4502/libs/granite/security/post/authorizables
  • Add a Property to an existing User using cURL AEM command
curl -u admin:admin -Fprofile/age=30 http://localhost:4502/home/users/a/ankur.rw.html
  • Create a User with a profile using cURL AEM command
curl -u admin:admin -FcreateUser=testuser -FauthorizableId=ankura -Frep:password=ankur -Fprofile/gender=male http://localhost:4502/libs/granite/security/post/authorizables
  • Create a new User as a member of a Group using cURL AEM command
curl -u admin:admin -FcreateUser=testuser -FauthorizableId=testuser -Frep:password=abcd123 -Fmembership=contributor http://localhost:4502/libs/granite/security/post/authorizables
  • Add User to a Group using cURL AEM command
curl -u admin:admin -FaddMembers=testuser http://localhost:4502/home/groups/t/testGroup.rw.html
  • Remove a User from a Group using cURL AEM command
curl -u admin:admin -FremoveMembers=testuser http://localhost:4502/home/groups/t/testGroup.rw.html
  • Set a User’s Group Memberships  using cURL AEM command
curl -u admin:admin -Fmembership=contributor -Fmembership=testgroup http://localhost:4502/home/users/t/testuser.rw.html
  • Delete user and Group using cURL AEM command
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 using cURL AEM command
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/[email protected]

AEM Package Management cURL commands:

Below are the AEM cUrl commands for Managing Packages in AEM  :-

  • AEM cURL command for accessing Help Menu
curl -u admin:admin http://localhost:4502/crx/packmgr/service.jsp?cmd=help
  • AEM cURL command to get list of all the packages in your AEM instance
curl -u admin:admin http://localhost:4502/crx/packmgr/service.jsp?cmd=ls
  • Build or Rebuild an existing package using AEM cURL command
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/samplepackage.zip?cmd=build

Note:- my_packages/samplepackage.zip is the path of the package which need to be build

  • Delete a package using AEM cURL command
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/samplepackage.zip?cmd=delete
  • Install a package using AEM cURL command
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/samplepackage.zip?cmd=install
  • Uninstall a package using AEM cURL command
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/samplepackage.zip?cmd=uninstall
  • Download a package into local filesystem using AEM cURL command
curl -u admin:admin http://localhost:4502/etc/packages/my_packages/samplepackage.zip > <local filepath>
  • Upload but don’t install a package from File system using AEM cURL command
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 a package from File system using AEM cURL command
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

AEM OSGI Bundle Management cURL commands:

Below are the AEM cUrl commands for OSGI Bundle Management in AEM  :-

  • Build a Bundle using AEM cURL command
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 using AEM cURL command
curl -u admin:admin http://localhost:4502/system/console/bundles/com.day.sample -Faction=start
  • Stop a Bundle using AEM cURL command
curl -u admin:admin http://localhost:4502/system/console/bundles/com.day.sample -Faction=stop
  • Install a Bundle from File system using AEM cURL command
curl -u admin:admin -F action=install -F bundlestartlevel=20 -F bundlefile=@"<path of samplejar.jar>" http://localhost:4502/system/console/bundles

AEM JCR Query builder cURL commands:

Below are the AEM cUrl commands for JCR search or JCR query builder in AEM  :-

  • Find an Asset from the JCR using AEM cURL command
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"

AEM Backup cURL commands:

Below are the AEM cUrl commands for OSGI Bundle Management in AEM  :-

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

Note:- You can also initiate backup through AEM Backup Console.

AEM JCR Node Manangement cURL commands:

Below are the AEM cUrl comands for JCR Node Management in AEM  :-

  • Delete a Node using AEM cURL command
curl -X DELETE http://localhost:4502/content/geometrixx/en/products/jcr:content/par/flash -u admin:admin

Note:- You can delete any directory, node and siteusing above command, text after jcr:content is nodeName on which you need to perform action par/flash = Node Name

  • Create or Add a Node using AEM cURL command
curl --data jcr:primaryType=nt:unstructured --user admin:admin http://localhost:4502/content/geometrixx/en/toolbar/testNode

AEM Replication cURL commands:

Below are the AEM cUrl commands used for replication in AEM  :-

  • Activate Page using cURL command
curl -u admin:admin -X POST -F path="/content/geometrixx/en/pag" -F cmd="activate" http://localhost:4502/bin/replicate.json
  • Deactivate Page using cURL command
curl -u admin:admin -X POST -F path="/content/geometrixx/en/pag" -F cmd="deactivate" http://localhost:4502/bin/replicate.json
  • Tree Activation(entire tree structure) using cURL command
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

Note:- Change Path variable in script to activate or deactivate respective page.

AEM Page Management cURL commands:

Below are the AEM cUrl commands used for page management in AEM  :-

  • Lock a Page using AEM cURL command
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 using AEM cURL command
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 or Move a Page using AEM cURL command
curl -u admin:admin -F:operation=copy -F:dest=/content/geometrixx/en/products/contacts http://localhost:4502/content/geometrixx/en/toolbar/contacts
 

Few AEM Miscellaneous cURL Commands:

Below are the few miscellaneous AEM cUrl commands :-

  • Datastore Garbage Collection  using AEM cURL command
curl -u admin:admin -X POST http://localhost:4502/system/console/jmx/com.adobe.granite:type=Repository/op/runDataStoreGarbageCollection/java.lang.Boolean
  • Tar Optimization  using AEM cURL command
curl -u admin:admin -X POST http://localhost:4502/system/console/jmx/com.adobe.granite:type=Repository/op/startTarOptimization/
  • Flush Dispatcher Cache  using AEM cURL command
curl -H "CQ-Action: Flush" -H "CQ-Handle: /content/geometrixx/en/products" -H "CQ-Path:/content/geometrixx/en/products" -H "Content-Length: 0" -H "Content-Type: application/octet-stream" http://dispatcher-server-hostname:port/dispatcher/invalidate.cache

Using JQ to log results from curls command and maintain logs.

JQ is a lightweight and flexible command-line JSON processor. Download jq

For example we want to get all assets based on a property under a specific folder and if that property is not present , then update it with node path on asset metadata in dam:newProperty and log them for further analysis.

#!/bin/bash -x
log=log.txt
o="$IFS"
IFS=$(echo -en "\n\b")
for ap in `curl -v -u "admin:admin" "http://localhost:4502/bin/querybuilder.json?path=/content/dam/we-retail/en/features&p.hits=selective&type=dam%3aAsset&property=jcr:content/metadata/dam%3anewProperty&property.operation=not&p.limit=-1" | ./jq-win64.exe '.hits[]."jcr:path"' | tr -d '"' | tr -d '\r'`; do
    cleanap=`echo "$ap" | sed 's/ /\%20/g'`
	responseCode=`curl -v -u "admin:admin" "http://localhost:4502$cleanap/jcr:content/metadata" -w '%{http_code}' --data "dam:newProperty=$ap"`;
	if [ "$responseCode" != "200" ]; then
		echo "`date` -- WARN: curl returned $responseCode status, could not modify $ap... cleaned URL was \"$cleanap\"" | tee -a $log
	else
		echo "`date` -- INFO: update property for $ap" | tee -a $log
	fi
done
IFS="$o"

In order to run above command, save it as .sh file and put jq-win64.exe in the same folder where your script file is present.

Above script mentioned should be executed using windows machine ,Now there is catch on how to run this script. By default when we run this script using Git bash. It adds system path along with node path as shown below

In order to avoid system path to be added to your custom property run above script using below command.

MSYS_NO_PATHCONV=1 ./yourscriptfilename.sh
Spread the love

Leave a Reply to Yoni Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.