Computing : Upload to Confluence using curl

It is possible to post content, for example updating an existing page, in a scripted fashion. A simple recipe could look like this:

# to upload to confluence you need a cookie. In batch-jobs you need to generate the cookie beforehand
# first check if an existing cookie is still valid. For example
cookie=$HOME/.ssh/confluence.cookie
test=0
if [[ -f $cookie ]]; then
  test=$( curl  -b $cookie --silent "https://confluence.desy.de/<some-page-requiring-login>" | wc -l )
fi
# if no or no valid cookie exists get a new one. Requires password.
if [[ $test -lt 20 ]]; then
  curl -u $USER -c $cookie "https://confluence.desy.de/dologin.action" -s --output /dev/null
fi

# define the page you'd like to update & fetch the meta-data for it. It'll be json
Title="Maxwell%20Hardware"
spaceKey="IS"
URL="https://confluence.desy.de/rest/api/content?title=${Title}&spaceKey=${spaceKey}"
URLmeta=$(curl -s -b $cookie -X GET "${URL}&expand=version")

# extract version and API id
version=$(echo $URLmeta | python -mjson.tool | jq '.results[].version.number')
pageID=$(echo $URLmeta | python -mjson.tool | jq '.results[].id' | tr -d '"' )
new=$(($version + 1))

# compose the new page. lets assume you compose the new content into a temporary file, but a string would do as well of course:
cat <<EOF>upload.tmp
{"id":"${pageID}","type":"page", "title":"${Title}","space":{"key":"$spaceKey"},"body":{"storage":{"value":"
EOF
cat <your-new-page-content> >> upload.tmp
cat <<EOF>>upload.tmp
","representation":"storage"}}, "version":{"number":${new}}}
EOF
cat upload.tmp  | tr -d '\n' > upload.tmp3

# update the page:
curl -s -b $cookie -X PUT -H 'Content-Type: application/json' -H 'Accept: application/json' -d @upload.tmp3 "https://confluence.desy.de/rest/api/content/${pageID}" | python -mjson.tool
rm -f upload.tmp*