How To Use a Bash Script with Curl To Send Content Page Changes to SCP
This example demonstrates how to use the SoftSlate Cloud Platform API to add and/or update content pages on your SCP site, from a file on your local filesystem. The scenario is, you would like to maintain some of your site content in a system that stores files locally (for example, using a desktop application that you are familiar with). When it comes time to upload the content, instead of logging into SCP, finding the Content Page to update, and copying and pasting the changes, you could simply fire off this script (or something like it).
The script is a Bash script that uses Perl to manipulate the content a little bit before sending it to SCP, and curl to actually perform the request.
xxxxxxxxxx
if [ $# -eq 0 ]; then echo "Usage: $0 <file_path>" exit 1fi
file_path=$1
if [ ! -f "$file_path" ]; then echo "Error: $file_path is not a valid file." exit 1fi
echo "file path:"echo "$file_path"
# Pull the name out of the file pathcontentpage_name=$(perl -e '$str = shift; $str =~ /\/([^\/]*)\.html/; print "$1\n";' "$file_path")
# Generate the code so it can edit the record too.contentpage_code=$(echo "$contentpage_name" | perl -pe 's/<.*?>//g')contentpage_code=$(echo "$contentpage_code" | perl -pe 's/[^a-zA-Z0-9_]+/_/g')contentpage_code=${contentpage_code%?} # remove the last - character; unknown why it's there
# Optionally, place under a parent content pageparent_content_page_id="49"
echo "cn: [$contentpage_name]"echo "cc: [$contentpage_code]"
# Read file contents into a variablefile_contents=$(cat "$file_path")
# We just want the <body> contentsfile_contents=$(echo "$file_contents" | perl -0777 -ne 'print $1 if /<body.*?>(.*?)<\/body>/is')
# Escape stuff to make the JSON happy.file_contents=$(echo "$file_contents" | perl -pe 's/\\/\\\\/g') # escape all the escape charactersfile_contents=$(echo "$file_contents" | perl -pe 's/"/\\"/g') # escape all the double quotesfile_contents=$(echo "$file_contents" | perl -pe 's/\n/\\n/g')file_contents=$(echo "$file_contents" | perl -pe 's/\t/\\t/g')contentpage_name=$(echo "$contentpage_name" | perl -pe 's/"/\\"/g')
# Construct the JSON request bodyjson_data="{ \"code\": \"$contentpage_code\", \"name\": \"$contentpage_name\", \"description\": \"$file_contents\", \"parentContentPageId\":$parent_content_page_id, \"contentpageType\":\"Content\" }"
# Credentials: create an API User in SCP with the contentPageEdit roleapi_domain=yourscpdomainname.comapi_user_and_pass=scp-api-user-username:scp-api-user-password
# Send the API requestcurl --insecure --location --request POST https://$api_domain/api/contentPage/v1/createOrUpdate --header 'Content-Type: application/json' -u "$api_user_and_pass" --data-raw "$json_data"
echo "ran curl"
To run this script, save it to a file, and make it executable. For our example, we have named the file "scpContentPageUpdate.sh". Call the script with one argument, which is the path to the file on your local system whose content you wish to use to update the content page on SCP. Here is an example:
./scpContentPageUpdate.sh /Users/me/content/My\ Content\ Page.html
The system will generate a code for the content page from the file name (My_Content_Page). The script calls the "createOrUpdate" endpoint. This means if a matching content page with the same code does not already exist, it will create a new one. If one does exist, it will update the "description" field with the content from the file.