Skip to content

Instantly share code, notes, and snippets.

@elbakramer
Last active March 4, 2018 09:05
Show Gist options
  • Save elbakramer/f0be7ec3dbdccea04344b668c1b6054a to your computer and use it in GitHub Desktop.
Save elbakramer/f0be7ec3dbdccea04344b668c1b6054a to your computer and use it in GitHub Desktop.
json rpc client in bash script
#!/bin/bash
rpcserver=""
rpcport=""
rpcuser=""
rpcpassword=""
method="help"
paramsjson="[]"
id="$(uuidgen)"
if [[ "$#" -gt "0" ]]; then
method="$1"
shift
if [[ "$#" -gt "0" ]]; then
params="$@"
paramsarray=( "$@" )
paramsbyline=$(IFS=$'\n'; echo "${paramsarray[*]}")
paramsjson=$(echo "$paramsbyline" | jq -R '[.]' | jq -s -c 'add')
fi
fi
data=$(jq \
--null-input \
--compact-output \
--arg jsonrpc "2.0" \
--arg method "$method" \
--argjson params "$paramsjson" \
--arg id "$id" \
'. | .["jsonrpc"]=$jsonrpc | .["method"]=$method | .["params"]=$params | .["id"]=$id')
response=$(echo "$data" | curl -sSL "http://${rpcuser}:${rpcpassword}@${rpcserver}:${rpcport}" \
-H "Content-Type: application/json" \
-d @-)
echo "$response" | jq -r 'if .error then .error else .result end'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment