cURL commands are an excellent way of transferring data with URLs. cURL commands are recommended to use APIs from command line terminals. It not just only allows us to make regular requests, but we can also post forms data, authenticate users, save responses to files and other related file manipulation operations. In simple words, curl is a command-line utility for transferring data from or to a server designed to work without user interaction. With curl, you can download or upload data using one of the supported protocols including HTTP, HTTPS, SCP, SFTP, and FTP.

Nevertheless, it also facilitates us to test RESTfull APIs.

cURL commands used

GET

The simplest and most common request/operation made using HTTP is to GET a URL. When using this command, we send a request and retrieve resources from a specific URL (a document in our case).

POST

The default request is a GET but if we have a requirement of using the same cURL command for submitting forms data, we need to use POST request. Therefore in this article, we will use the same POST command to submit text content. Please note that in order to post data, we use -d (or – data) parameter with the cURL command.

PUT

We may stumble upon a requirement to update existing data, so for that purpose, a PUT method is used. In order to accomplish this requirement, we need to use -X PUT in cURL command where X needs to be in upper case.

For further information, please visit cURL tutorial.

Note: when using cURL commands, ensure you do not put an extra space after the backslash \ else, the command will not be executed and errors will be displayed.

Setting up cURL on Mac

In this article, we are going to use cURL commands on macOS to insert and update Header Footer objects inside MS Word document using a terminal application. So in order to get started, the first thing is to install cURL on macOS. Open the Terminal application under Others category from the launchpad or, Press Command+Space and type Terminal and press enter/return key. The easiest way to install cURL is using Homebrew and for that reason, first, you need to set up homebrew on your mac machine. Type following command on command line terminal and press Enter key.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null

Now get the brew package for cURL using the following command.

 brew install curl 

Done! You can now use curl.

Authorize Aspose.Cloud account

In order to get started with Aspose.Cloud APIs, the first step is to authenticate your account and get JWT in response. If you do not have an account, you may create one on Aspose.Cloud dashboard or signup through your existing Google or GitHub account and start using our Cloud APIs.

// First get JSON Web Token for authentication
// Get App Key and App SID from https://dashboard.aspose.cloud/
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=[APP_SID]&client_secret=[APP_KEY]" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Aspose.Words Cloud

Aspose offers APIs to manipulate MS Word documents and you get an option to either use Cloud APIs, On-Premise APIs or, Cross-platform Apps to process Word documents. In this blog, we have emphasized over Cloud APIs which provide us the capabilities to create, manipulate and transform MS Word documents in the Cloud. No specific software download or installation required to work with MS Word files.

The Aspose.Cloud Product family provides a set of cloud SDKs targetting popular programming languages including .NET, Java, PHP, Python, Node.js, Android, C++, Ruby, Swift and Go. All these SDKs are a wrapper around Aspose.Words Cloud REST API and cURL commands are one of the simplest and easiest ways to access REST APIs over command prompt/terminal.

In this blog, we are emphasizing on using cURL commands to manipulate the Header and Footer section inside MS Word files while accessing Aspose.Words Cloud API.

Upload Word document

Once the authentication has been performed, the next step is to upload the file to cloud storage. By using the following cURL command, the file is uploaded to default storage under InputFolder.

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/storage/file/InputFolder" \
-H "accept: application/json" \
-H "authorization: Bearer <jwt token>" \
-F document=@BusinessLetter2.docx
Console output when file is uploaded to Cloud Storage

Success message when a file is uploaded to cloud storage.

Now in order to confirm the file presence on cloud storage, open My Files tab over dashboard.aspose.cloud, we can notice that a new folder named InputFolder containing BusinessLetter2.docx is appearing in the list.

Newly Created folder and file uploaded on Cloud Storage

Insert Header object

After uploading the document, we can either insert a new Header or Footer object or access an existing object and update its properties. Currently, Aspose.Words Cloud API supports the following types of Header and Footer objects.

The following command shows how to insert HeaderFirst object inside Word document already available in Cloud storage under InputFolder.

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/BusinessLetter2.docx/headersfooters?folder=InputFolder" \
-H "accept: multipart/form-data" \
-H "authorization: Bearer <jwt token>" \
-d "'HeaderFirst'" \
-H "Content-Type: application/json"

Once the above command is executed, the console lists the sections inside Word document.

The header and Footer sections usually contain text, images, etc and in the following command, we are going to insert a sample text inside the first paragraph inside the header object created above.

curl -v -X POST "https://api.aspose.cloud/v4.0/words/BusinessLetter2.docx/sections/0/headersFooters/1/paragraphs?folder=InputFolder" \
-H "accept: multipart/form-data" \
-H "authorization: Bearer <jwt token>" \
-d "{ 'Text': 'Aspose.Words Cloud .NET SDK' }" \
-H "Content-Type: application/json"
Console output after Text is inserted in the Header object. Observe the path of the text inserted.

Console output after Text is inserted in the Header object. Observe the path of the text inserted.

Update Text Formatting of the Header object

Once the text is inserted, we can update the text font properties. Since a word document may contain many text instances, so in order to update the text, we need to provide the exact path. So if we need to update the font properties for the above-inserted text which is inserted over sections/0/headersfooters/1/paragraphs/0/runs/0, then we need to use the following command.

Please note that now that we are going to update the font properties of an already placed objects, so we need to use the PUT command.

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/BusinessLetter2.docx/sections/0/headersFooters/1/paragraphs/0/runs/0/font?folder=InputFolder" \
-H "accept: multipart/form-data" \
-H "authorization: Bearer <jwt token>" \
-d "{ 'Bold':true, 'Size': 21.0, 'Name': 'Calibri',  'Color': { 'Web': '#f54269', 'Alpha': 0 } }" \
-H "Content-Type: application/json"

One of the important usages of Header and Footer objects is to display page number information and while using Aspose.Words Cloud, we can easily accomplish this requirement. The API also provides the capabilities to specify the location where PageNumber stamp needs to be displayed. As shown in command below, we have specified to add PageNumber information to be rendered on the Top-Right location of the page and also display it over the first page of the document.

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/BusinessLetter2.docx/PageNumbers?folder=InputFolder" \
-H "accept: multipart/form-data" \
-H "authorization: Bearer <jwt token>" \
-d "{ 'Format': '{PAGE} of {NUMPAGES}', 'Alignment': 'right', 'IsTop': false, 'SetPageNumberOnFirstPage': true }" \
-H "Content-Type: application/json"

Similar to font properties update to Header instance, we can also update the font properties of Footer object which contains PageNumber information. As the page number information is comprised of three-run objects (runs/0, runs/2, and runs/3), so we need to update the font update command three times for each run instance.

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/BusinessLetter2.docx/sections/0/headersFooters/3/paragraphs/1/runs/3/font?folder=InputFolder" \
-H "accept: multipart/form-data" \
-H "authorization: Bearer <jwt token>" \
-d "{ 'Bold':true, 'Size': 21.0, 'Name': 'Arial', 'Shadow': true, 'Color': { 'Web': '#f5b642', 'Alpha': 0 } }" \
-H "Content-Type: application/json"

Once all the above commands are executed, the resultant document will look similar to the preview above.

For your reference, the sample documents are also attached