REST Login to Confluence with cURL

To access Confluence via its REST API with cURL you typically need to authenticate. Learn how to login with cURL and avoid some common security pitfalls.

Parent
Audience
Level of Experience
Expected Duration
15 min
Tags
Type

The Confluence Server REST API can be used with the REST API Browser when it is enabled / installed and when you are logged in to Confluence with your browser. But when you would like to execute REST API calls via cURL (a command-line tool for transferring data using various protocols) you have to login via cURL to use most resources of the Confluence REST API.

This tip shows you how to login to Confluence using cURL.

Prerequisites

Download and Install cURL:

https://curl.haxx.se/download.html

Or install direct e.g in ubuntu:

apt-get install curl

Test your Installation:

curl --version
curl 7.47.0 ...

Executing REST calls

Suppose you want to make some REST calls to your Confluence installation: e.g. https://www.example.com/confluence/rest/api

REST Call with Username and Password

According to the Confluence REST API Examples you could pass the credentials (username and password) directly to cURL for each call:

Simple REST call (login username:password)
curl -u admin:admin https://www.example.com/confluence/rest/api/content/

REST Call (Password will be prompted for)

Another option is to enter the password each time you make a REST call by omitting the password in your call:

Simple REST call (login username | password will be prompted)
curl -u admin https://www.example.com/confluence/rest/api/content/
Enter host password for user 'admin':

Now cURL prompts for the password and you can enter it unseen for others. The downside using this is you have to enter the password for every REST call again and again.

WARNING MESSAGE

REST Call using Config File for Credentials

Creating the cURL config file:

Creating the config file
echo "--user admin:XXXX" > ~/curl-password-config.config
chmod go-r ~/curl-password-config.config

Have a look at your file:

The config file
cat ~/curl-password-config.config
--user admin:XXXX

Now edit the file and replace the XXXX placeholder with your password.

Use the config file for your next REST call:

Simple REST call (using config file)
curl --config ~/curl-password-config.config https://www.example.com/confluence/rest/api/content/

REST Call using .netrc for Credentials

For more information on the .netrc file please read the GNU Documentation on the .netrc file.

 Creating the .netrc file | adding a new machine to the .netrc file:

Creating the .netrc file
echo -e "machine www.example.com \n\tlogin admin\n\tpassword XXXX" >> ~/.netrc
chmod go-r ~/.netrc

Have a look at your file:

The config file
cat ~/.netrc
machine www.example.com
        login admin
        password XXXX

Now edit the file and replace the XXXX placeholder with your password.

Use the .netrc file for your next REST call:

Simple REST call (using config file)
curl -n https://www.example.com/confluence/rest/api/content/

REST Call using a Session Cookie

One more option you could use is to login to Confluence once, use cookies, store the session cookie and re-use this session cookie for subsequent calls (at least until the session timeout fires):

Login and store session cookie
curl -s --cookie ~/privatedir/confluence.cookie --cookie-jar ~/privatedir/confluence.cookie \
 --data "os_username=admin"  --data "os_password=admin" \
 --data "formname=loginform" --data "login='Log In'" https://www.example.com/confluence/dologin.action -o /dev/null

Now make some REST calls using the cookie:

Simple REST call using session cookie
curl  -s --cookie ~/privatedir/confluence.cookie https://www.example.com/confluence/rest/api/content/

REST Call using a Session Cookie by creating a Config File on the Fly

But there is (at least) one more option you could use. The option to build a config file on the fly, read it from STDIN, read the password from STDIN and use session cookies can be combined efficiently:

Login and store session cookie by creating a config file on the fly
{ echo -n  '--data "os_password='; read a && echo -n $a ; echo -n '"'; } | curl -K - \
 -s --cookie ~/privatedir/confluence.cookie --cookie-jar ~/privatedir/confluence.cookie \
 --data "os_username=admin" \
 --data "formname=loginform" --data "login='Log In'" \ 
 https://www.example.com/confluence/dologin.action -o /dev/null

It works by building the --data "os_password=password" part of the options passed to cURL on the fly and by reading the password from STDIN.

Now make some REST calls using the cookie:

Simple REST call using session cookie
curl  -s --cookie ~/privatedir/confluence.cookie https://www.example.com/confluence/rest/api/content/