Automate your cURL Completely

Hi Friends,

Since the past few days, I have explored cURL quite a lot and I just loved the power it gives to the programmers. Through this post , I would try to illustrate the actual purpose and use of cURL : to automate almost everything in AEM. You can also schedule a cURL command to be executed after a period of time. Its simple and easy to learn. I hope you will utilize cURL explicitly in your projects.

Where Should I use cURL in AEM?

In general , cURL has applicability in the following cases :

  • If you’re in need of debugging HTTP responses and finding out more information about what the server is sending you? cURL is your friend.
  • If you want to transfer files using from various protocols such as HTTP, FTP, and others, with nothing more but the command line? cURL is your beloved friend.
  • If you are looking to automate your process of acquiring or sending data ? cURL is your close buddy.

In respect to AEM,

  • If you want to initiate Backups : complete or partial (via packages), cURL will help you.
  • If you need to initiate JCR modifications , User Management , Page Management, OSGi Management , Package Management ? cURL is all you need.

So if you answered yes to any of the above, then check out the articles below for more information about cURL and how you can use it to your advantage.

Automate cURL commands

If you want to run a command directly without typing it on your command prompt , you may use a batch script (for Windows) or bash script (for Linux). I will help you write your first batch script with simple commands , which you may use to build complex logic afterwards as per your need.

Simple Syntax for writing Batch File

Its quite simple to write a batch file, just create a new file in your desktop/ folder and rename the file with .bat extension. There are a few quick commands which will help you to begin with Batch scripts:

  • TITLE – The Window name for the BAT file.
  • ECHO – the “print” statement for BAT files. Anything following the word ECHO will be displayed in the command prompt as text, on its own line.
  • ECHO OFF – BAT writers typically put this at the beginning of their files. It means that the program won’t show the command that you told it to run while it’s running – it’ll just run the command.
  • PAUSE – This outputs the “press any key to continue…” message that you’ve seen all too many times. It’s helpful because it pauses the BAT file execution until the user tells it to go again. If you don’t put this in your program, everything will speed by and end before you can see it.
  • CLS – Clears the DOS window (helpful if things get too cluttered!).
  • IPCONFIG – Outputs a lot of network information into your DOS box
  • PING – Pings an IP, letting you know if your computer was able to contact it. This command also returns the latency (ping time) and by default pings three times.

Write your first Batch file with cURL

You may use any of the command I shared in previous post , to create your batch file. I am doing it with the most simplest one.

::CMD will no longer show us what command it’s executing(cleaner)
ECHO This is my first cURL script. I will be exploring it efficiently from now on.
:: Print some text
curl --help
:: Add your custom cURL command here.
:: Give the user some time to see the results. Because this is our last line, the program will exit and the command window will close once this line finishes.

Now you are done with your first cURL based Batch file. Go ahead and double click it to execute

Automate it Completely

Bill Gates once said he would always ‘hire a lazy person to do a difficult job’ at Microsoft ‘because a lazy person will find an easy way to do it.

 Yes, I am too lazy to even manually click the batch file . I want it to be self executed after a particular time interval. This will be a powerful way to do a repeated task in AEM- eg a daily backup package, a daily backup, a daily update of some content from a particular folder , etc. To do this you need to follow a few steps and you will not have to even execute it yourself.

  • After creating your batch file , make sure that it is working fine as expected.
  • Go to Windows> Start> Search for Task Scheduler (Windows 7)
  • Create a Basic Task and fill the necessary details.
  • Setup the time period in which you need to execute the batch file.
  • At the Action Tab, Select a Program/Script and choose the path for your Batch file.
  • Click Finish and you are done .

You may refer to this link, for screenshots of the above process 

I hope this post will help you guys understand the real purpose of cURL . I am sure you will find it a very valuable tool in the days to come.


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s