Last active
March 4, 2018 09:05
-
-
Save elbakramer/f0be7ec3dbdccea04344b668c1b6054a to your computer and use it in GitHub Desktop.
json rpc client in bash script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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