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
- While clicking on Start keep a watch at the POST call from the Firebug Console>Network.
- In every functionality you will see a call to server: either a POST or GET. Here you will find a POST call to the
- URL: http://localhost:4502/libs/granite/backup/content/admin/backups/
- Parameters: _charset_=utf-8&force=false&installDir=C%3A%5CAEM+Dev%5CAEM&target=C%3A%5Csample%5CBackup&delay=1000
- 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 !!
I would like to know if we can use curl commands in our java files. Like can we write a simple utility using java and use curl commands to do the similar operations. If yes could you please help me to do that.
LikeLike
Yes its possible . Please try using http://stackoverflow.com/questions/31964264/covert-curl-to-java-code .
Regards
Hashim
LikeLike
Hi Hashim,
I want to automate the package creation process. I have two text files- one with the normal filters and another with the rules that I want to exclude.There should be some one to one relation between two files. For example- if filter is /content/dam and first line of rule is to exclude one image /exampleImage. How to proceed with this?
LikeLike
Hi Hashim
curl -u admin:%pass% -Fhome=http://%ip%:%port%/home/groups/g/grp-hdnet -FcreateGroup=group1 -FauthorizableId=grpid -Fprofile/givenName=”Group Name” http://localhost:4502/libs/granite/security/post/authorizables
This curl script is working. I wanted to check is there any parameter which I can pass and provide and to mention the path where which group has to be created like
http://%ip%:%port%/home/groups/g/grp-hdnet
Now groups are created in random paths like :- /home/groups/R/R-Az7OVMSTQ2HdEqS10k
LikeLike
I got the option: -FintermediatePath=/home/groups/g/grp-hdnet
I figured it out from here:- https://jackrabbit.apache.org/api/2.8/org/apache/jackrabbit/api/security/user/UserManager.html#createGroup(java.security.Principal)
Great Helpful post for all Curl commands.
LikeLike
Hi Hashim,
Is there a option if I have to remove multiple packages using curl excluding any specific package.
LikeLike