January 4, 2021
Estimated Post Reading Time ~

Using cURL with AEM

Administrators often need to automate or simplify common tasks within any system. In AEM for example, managing users, installing packages, and managing OSGi bundles are tasks that must be commonly done.

Because of the RESTful nature of the Sling framework upon which AEM is built, most tasks can be reduced to a URL call. cURL can be used to execute such URL calls and can be a useful tool for AEM administrators.

What is cURL
cURL is an open-source command-line tool used to perform URL manipulations. It supports a wide range of internet protocols including HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, LDAP, DAP, DICT, TELNET, FILE, IMAP, POP3, SMTP, and RTSP.

cURL is a well-established and widely-used tool for getting or sending data using the URL syntax and was originally released in 1997. The name cURL originally meant "see URL."

Because of the RESTful nature of the Sling framework upon which AEM is built, most tasks can be reduced to a URL call, which can be executed with cURL. Content manipulation tasks such as activating pages, and starting workflows as well as operational tasks such as package management and managing users can be automated using cURL. In addition, you can create your own cURL commands for most tasks in AEM.

Note:
Any AEM command executed through cURL has to be authorized just as any user to AEM. All ACLs and access rights are respected when using cURL to execute an AEM command.

Downloading cURL
cURL is a standard part of macOS and some Linux distros. However, it is available for almost every operating system. The latest downloads can be found at https://curl.haxx.se/download.html.

cURL's source repository can be found on GitHub as well.

Building a cURL-Ready AEM Command
cURL commands can be built for most operations in AEM such as triggering workflows, checking OSGi configurations, triggering JMX commands, creating replication agents, and much more.

To find the exact command you need for your particular operation, you need to use the developer tools in your browser to capture the POST call to the server when you execute the AEM command.

The following steps describe how to do this using the creation of a new page within the Chrome browser as an example.

Prepare the action you wish to invoke within AEM. In this case, we have proceeded to the end of the Create Page wizard, but have not yet clicked Create.

Start the developer tools and select the Network tab. Click the Preserve log option before clearing the console.



Click Create in the Create Page wizard to actually create the workflow.

Right-click on the resulting POST action and select Copy -> Copy as cURL.

Copy the cURL command to a text editor and remove all headers from the command, which start with -H (highlighted in blue in the image below) and add the proper authentication parameter such as -u admin:admin.

Execute the cURL command via the command line and view the response.
Common Operational AEM cURL Commands

Here is a list of AEM cURL commands for common administrative and operational tasks.

Note:
The following examples assume that AEM is running on localhost on port 4502 and uses the user admin with password admin. Additional command placeholders are set in angle brackets.

Package Management
CREATE A PACKAGE

curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/mycontent.zip?cmd=create -d packageName=<name> -d groupName=<name>

PREVIEW A PACKAGE
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/mycontent.zip?cmd=preview

LIST PACKAGE CONTENT
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/console.html/etc/packages/mycontent.zip?cmd=contents

BUILD A PACKAGE
curl -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/mycontent.zip?cmd=build

REWRAP A PACKAGE
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/mycontent.zip?cmd=rewrap

RENAME A PACKAGE
curl -u admin:admin -X POST -Fname=<New Name> http://localhost:4502/etc/packages/<Group Name>/<Package Name>.zip/jcr:content/vlt:definition

UPLOAD A PACKAGE
curl -u admin:admin -F cmd=upload -F force=true -F package=@test.zip http://localhost:4502/crx/packmgr/service/.json

INSTALL A PACKAGE
curl -u admin:admin -F cmd=install http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/test.zip

UNINSTALL A PACKAGE
curl -u admin:admin -F cmd=uninstall http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/test.zip

DELETE A PACKAGE
curl -u admin:admin -F cmd=delete http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/test.zip

DOWNLOAD A PACKAGE
curl -u admin:admin http://localhost:4502/etc/packages/my_packages/test.zip

User Management
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 A 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 MEMBERSHIP
curl -u admin:admin -Fmembership=contributor -Fmembership=testgroup http://localhost:4502/home/users/t/testuser.rw.html

DELETE A USER
curl -u admin:admin -FdeleteAuthorizable= http://localhost:4502/home/users/t/testuser


DELETE A GROUP
curl -u admin:admin -FdeleteAuthorizable= http://localhost:4502/home/groups/t/testGroup

Backup
See Backup and Restore for details.

OSGi
STARTING A BUNDLE
curl -u admin:admin -Faction=start http://localhost:4502/system/console/bundles/<bundle-name>

STOPPING A BUNDLE
curl -u admin:admin -Faction=stop http://localhost:4502/system/console/bundles/<bundle-name>

Dispatcher
INVALIDATE THE CACHE
curl -H "CQ-Action: Activate" -H "CQ-Handle: /content/test-site/" -H "CQ-Path: /content/test-site/" -H "Content-Length: 0" -H "Content-Type: application/octet-stream" http://localhost:4502/dispatcher/invalidate.cache

EVICT THE CACHE
curl -H "CQ-Action: Deactivate" -H "CQ-Handle: /content/test-site/" -H "CQ-Path: /content/test-site/" -H "Content-Length: 0" -H "Content-Type: application/octet-stream" http://localhost:4502/dispatcher/invalidate.cache

Replication Agent
CHECK THE STATUS OF AN AGENT

curl -u admin:admin "http://localhost:4502/etc/replication/agents.author/publish/jcr:conten t.queue.json?agent=publish"
http://localhost:4502/etc/replication/agents.author/publish/jcr:content.queue.js on?agent=publish

DELETE AN AGENT
curl -X DELETE http://localhost:4502/etc/replication/agents.author/replication99 -u admin:admin

CREATE AN AGENT
curl -u admin:admin -F "jcr:primaryType=cq:Page" -F "jcr:content/jcr:title=new-replication" -F "jcr:content/sling:resourceType=/libs/cq/replication/components/agent" -F "jcr:content/template=/libs/cq/replication/templates/agent" -F "jcr:content/transportUri=http://localhost:4503/bin/receive?sling:authRequestLogin=1" -F "jcr:content/transportUser=admin" -F "jcr:content/transportPassword={DES}8aadb625ced91ac483390ebc10640cdf"http://localhost:4502/etc/replication/agents.author/replication99

PAUSE AN AGENT
curl -u admin:admin -F "cmd=pause" -F "name=publish" http://localhost:4502/etc/replication/agents.author/publish/jcr:content.queue.js on

CLEAR AN AGENT QUEUE
curl -u admin:admin -F "cmd=clear" -F "name=publish" http://localhost:4502/etc/replication/agents.author/publish/jcr:content.queue.js on

Communities
ASSIGN AND REVOKE BADGES
See Communities Scoring and Badges for details.
See Scoring and Badges Essentials for details.

MSRP REINDEXING
See MSRP - MongoDB Storage Resource Provider for details.

Security
ENABLING AND DISABLING CRX DE LITE
See Enabling CRXDE Lite in AEM for details.

Data Store Garbage Collection
See Data Store Garbage Collection for details.

Analytics and Target Integration
See Opting Into Adobe Analytics and Adobe Target for details.

Single Sign-On
SEND TEST HEADER
See Single Sign-On for details.

Common Content Manipulation AEM cURL Commands

Here is a list of AEM cURL commands for content manipulation.
Note:
The following examples assume that AEM is running on localhost on port 4502 and uses the user admin with password admin. Additional command placeholders are set in angle brackets.

Page Management
PAGE ACTIVATION
curl -u admin:admin -X POST -F path="/content/path/to/page" -F cmd="activate" http://localhost:4502/bin/replicate.json

PAGE DEACTIVATION
curl -u admin:admin -X POST -F path="/content/path/to/page" -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 http://localhost:4502/etc/replication/treeactivation.html

LOCK PAGE
curl -u admin:admin -X POST -F cmd="lockPage" -F path="/content/path/to/page" -F "_charset_"="utf-8" http://localhost:4502/bin/wcmcommand

UNLOCK PAGE
curl -u admin:admin -X POST -F cmd="unlockPage" -F path="/content/path/to/page" -F "_charset_"="utf-8" http://localhost:4502/bin/wcmcommand

COPY PAGE
curl -u admin:admin -F cmd=copyPage -F destParentPath=/path/to/destination/parent -F srcPath=/path/to/source/location http://localhost:4502/bin/wcmcommand

Workflows
See Interacting with Workflows Programmatically for details.

Sling Content
CREATE A FOLDER
curl -u admin:admin -F jcr:primaryType=sling:Folder http://localhost:4502/etc/test

DELETE A NODE
curl -u admin:admin -F :operation=delete http://localhost:4502/etc/test/test.properties

MOVE A NODE
curl -u admin:admin -F":operation=move" -F":applyTo=/sourceurl" -F":dest=/target/parenturl/" http://lcoalhost:4502/content

COPY A NODE
curl -u admin:admin -F":operation=copy" -F":applyTo=/sourceurl" -F":dest=/target/parenturl/" http://lcoalhost:4502/content

UPLOAD FILES USING SLING POSTSERVLET
curl -u admin:admin -F"*=@test.properties" http://localhost:4502/etc/test

UPLOAD FILES USING SLING POSTSERVLET AND SPECIFYING NODE NAME
curl -u admin:admin -F"test2.properties=@test.properties" http://localhost:4502/etc/test

UPLOAD FILES SPECIFYING A CONTENT TYPE
curl -u admin:admin -F "*=@test.properties;type=text/plain" http://localhost:4502/etc/test



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.