Configuring remote operation with curl

Operational processes and maintenance tasks can be operated remotely from a central data center environment by using the IBM® Safer Payments application programming interface (API) and a shell script, such as curl.

For more information about API requests, see the IBM Safer Payments API Reference Guide. To use IBM Safer Payments API calls from a shell script, it is best to use ​curl with cookie storage.

The curl requests are similar to the following:

curl --cookie-jar tmp_cookie http://_IPADDRESS_:_PORT_/?{"request":"login","data":{"login":"_LOGIN_","password":"_PASSWORD_"}}

curl --cookie  tmp_cookie http://_IPADDRESS_:_PORT_/?{"request":"setOffline","uid":_INSTANCEUID_}

curl --cookie  tmp_cookie http://_IPADDRESS_:_PORT_/?{"request":"isOffline","uid":_INSTANCEUID_} | grep "INSTANCE_IS_OFFLINE"

curl --cookie  tmp_cookie http://_IPADDRESS_:_PORT_/?{"request":"setOnline","uid":_INSTANCEUID_}

You must encode the URL. If you do not, an error is issued. IBM Safer Payments returns, in turn, an encoded URL result. Consider the following example workflow:


# Get a Safer Payments token (required for most API calls)

user=MyUsername

password="MyPassword"

port=8001

url=http://saferpayments.ibm.com

request='{"request":"login","data":{"login":"'$user'","password":"'$password'"}}'

response=$(curl -G -j -c ./cookies -sX GET $url:$port --data-urlencode ''$request'')

tokend=$(jq -r '.downloadToken' <<< $response)

tokenc=$(jq -r '.csrfToken' <<< $response)

# Optionally, view the results

echo $response

# Get details about user ID (UID) 998

request='{"request":"get","uid":998}'

curl -G -v -b ./cookies -k -sX GET $url:$port --data-urlencode ''$request'' -H 'downloadToken: '$tokend'' -H 'csrfToken: '$tokenc'' 

# Format (pretty-print) the JSON output

curl -G -v -b ./cookies -k -sX GET $url:$port --data-urlencode ''$request'' -H 'downloadToken: '$tokend'' -H 'csrfToken: '$tokenc'' | python -m json.tool

Alternatively, you can send already encoded URLs. For example, setOffline, which is used to disable all interfaces on an instance, would be:

curl --cookie-jar tmp_cookie http://_IPADDRESS_:_PORT_/%3F%7B%22request%22%3A%22login%22%2C%22data%22%3A%7B%22login%22%3A%22_LOGIN_%22%2C%22password%22%3A%22_PASSWORD_%22%7D%7D
curl --cookie  tmp_cookie http://_IPADDRESS_:_PORT_/%3F%7B%22request%22%3A%22setOffline%22%2C%22uid%22%3A_INSTANCEUID_%7D
OFFLINE=1
isOffline () {
  curl --cookie tmp_cookie http://_IPADDRESS_:_PORT_/%3F%7B%22request%22%3A%22isOffline%22%2C%22uid%22%3A_INSTANCEUID_%7D% | grep "INSTANCE_IS_OFFLINE"
  OFFLINE=$?
}
isOffline
while [ $OFFLINE = 1 ]
do
  isOffline
  sleep 1
done;

For setOnline, which is used to reactivate all enabled interfaces after they were taken offline:

curl --cookie-jar tmp_cookie http://_IPADDRESS_:_PORT_/%3F%7B%22request%22%3A%22login%22%2C%22data%22%3A%7B%22login%22%3A%22_LOGIN_%22%2C%22password%22%3A%22_PASSWORD_%22%7D%7D
curl --cookie  tmp_cookie http://_IPADDRESS_:_PORT_/%3F%7B%22request%22%3A%22setOnline%22%2C%22uid%22%3A_INSTANCEUID_%7D

_LOGIN_ should be a user login with credentials to change cluster settings.

_PASSWORD_ is the login's password (plain text) .

_INSTANCEUID_ is the number of the &uid= field in the url, when you click an instance in the Cluster menu.

If you use the Secure Socker Layer (SSL) encrypted API interface, replace http with https in the URL. For not checking the SSL certificate, add -k to the curl call curl -k --cookie-jar tmp_cookie https://.… .