Skip to content

Instantly share code, notes, and snippets.

@orip
Last active September 5, 2022 09:39
Show Gist options
  • Save orip/f6a3130d0ee931cf6618a8ed4958b427 to your computer and use it in GitHub Desktop.
Save orip/f6a3130d0ee931cf6618a8ed4958b427 to your computer and use it in GitHub Desktop.
helper script to make JSON-RPC (JRPC) 2.0 calls using curl
#!/bin/bash
usage_exit() {
echo "$0 <target-url> <jrpc-method> [jrpc-params] [extra-curl-args]"
exit 1
}
target="$1"
shift
method="$1"
shift
if [[ -z $target ]] || [[ -z $method ]]; then
usage_exit
fi
if [[ $# -gt 0 ]]; then
params="$1"
shift
else
params='{}'
fi
if [[ $# -gt 0 ]]; then
extra_curl_args=("$@")
else
extra_curl_args=()
fi
jrpc_id=$(hexdump -vn16 -e'4/4 "%08X" 1 "\n"' /dev/urandom)
echo "target: ${target}"
echo "method: ${method}"
echo "params: ${params}"
echo "id: ${jrpc_id}"
body='{"jsonrpc":"2.0","id":"'"${jrpc_id}"'","method":"'"${method}"'","params":'"${params}"'}'
set -ex
curl --insecure -H 'Content-Type: application/json' -d "${body}" "${target}" "${extra_curl_args[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment