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 !!

61 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

  6. Thanks for the great post Hashim!

    I was hoping to run a few curl commands from my desktop to localhost using a form and ajax.
    Since it’s on my desktop I figured that writing in HTML/JS would suffice. (No need to hide username, password, etc.)

    After doing this, I receive “authentication failed”. I think it has to do with CORS but do you know of ways to fix?

    $(“form”).on(“submit”, function(e) {
    e.preventDefault();
    //curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/samplepackage.zip?cmd=delete
    $.ajax({
    url: “http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/test.zip?cmd=delete”,
    method: “POST”,
    headers: {
    “Authorization”: “Basic ” + btoa(“admin:admin”)
    },
    success: function(data) {
    console.log(“success”);
    },
    error: function() {
    console.log(“failed”);
    }
    });
    });

    Like

  7. Hi Hashim,

    I need to take package of paths and its concepts (from cq:tags). How is it possible to include the paths from cq:tags or list those paths using curl command?

    Like

    • Hi Vyong,
      Is there an OOTB servlet to make this change ? Or is there a custom servlet which does that ? You can probably call that POST servlet / html call via cURL to perform this action.

      Like

      • Hi Hashim,
        Actually I can use CURL to modify the value for jcr:mixinTypes but it only let me update to 1 value. This property is Name[]. I want to add multiple values to it. The strange thing is I can do that with a different property for example I create a jcr:test with Name[] and add multiple values to it. It just didn’t work with jcr:mixinTypes

        Like

  8. Hi Hashim,

    I was recently having issues with the Tree Activation command. When I ran this command for a file with a special character (I am trying to replicate Spanish and French files) the file name would get encoded and then processed, causing the replication to fail. So, for example, the word “Navegación” gets encoded to “Navegación”. Since there is no file called “Navegación”, the command fails and the file does not get replicated. Do you know how I can disable encoding when I run curl commands?

    Like

  9. Pingback: About curl commands – Kaushik G

  10. Pingback: complete AEM. – Kaushik G

    • Hi,
      Yes you can do so. I would prefer to upload packages one by one though. instead of doing all at once as it would be a load to the server.
      Create a text file with list of packages.
      Place the packages in the same folder or any other folder.
      Now write a bash script to execute cURL commands one by one from the list of packages.

      Like

    • Hi, Yes sure, Read this to check Console to find out the curl command.
      there should be POST call when you click Cancel Inheritance from the dialog. You can recreate that POST with proper FORM data for that particular component node
      eg – on this page http://localhost:4502/editor.html/content/geometrixx/en/toolbar.html

      there is a POST call –
      Request URL: http://localhost:4502/content/geometrixx/en/toolbar/_jcr_content/rightpar
      Request Method: POST

      with form data:
      _charset_: utf-8
      ./sling:resourceType: foundation/components/iparsys
      ./jcr:lastModified:
      ./jcr:lastModifiedBy:
      ./inheritance: cancel
      ./inheritance@Delete:
      ./inheritance@Delete: true
      ./inheritance@TypeHint: String
      ./iparsys_fake_par/inheritance@Delete:
      ./iparsys_fake_par/inheritance@Delete: true
      ./iparsys_fake_par/inheritance@TypeHint: String
      ./iparsys_fake_par/sling:resourceType: foundation/components/iparsys/par

      Like

  11. Hi Hashim,

    I want to delete jar files under /apps/project/install/ using curl which will be used in a jenkins job. Is there a way to do it because the jar file name will change after every execution.

    Like

  12. Hi Hashim,
    Interesting stuff out here. Kudos!

    I am stuck while using cURL to rename multiple assets in DAM. I have made a cURL command that renames a single file (by capturing POST through Developer tools in Chrome).
    But, I need to use that command to rename around 1000s of files after they’re uploaded in DAM. Since, multiple renaming is not possible in TouchUI, we have to provide an automated solution to rename a large number of assets that’d otherwise be huge manual effort for the client.
    I was hoping you could suggest something like creating a batch to be used in curl or something. Here is the curl command to rename one asset in DAM-

    curl -u admin:admin ‘http://devfm1aemadts11.amr.c**p.i***l.com:4502/bin/wcmcommand’ –data ‘cmd=movePage&integrity=true&_charset_=utf-8&%3Astatus=browser&destParentPath=%2Fcontent%2Fdam%2Fdita-sandbox%2Fsample-2&destName=TESTDITASUCCESS.gif&srcPath=%2Fcontent%2Fdam%2Fdita-sandbox%2Fsample-2%2FTESTDITA.gif’ –compressed

    Thanks!

    Like

Leave a comment