Skip to content

Instantly share code, notes, and snippets.

@martin-braun
Last active March 10, 2024 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-braun/69d9cf77643c9bc89410487d7b1ba210 to your computer and use it in GitHub Desktop.
Save martin-braun/69d9cf77643c9bc89410487d7b1ba210 to your computer and use it in GitHub Desktop.
jrpc - Simple CLI tool for testing output from JSON RPC 2.0 services

JSON RPC CLI test tool

Usage

Usage: jrpc [OPTIONS]
Simple CLI tool for testing output from JSON RPC 2.0 services.

OPTIONS
  -V,   --version                 Prints the version of this script.
  -v,   --verbose                 Prints verbose information.
  -u=*, --url                     Specifies the URL to use, default is "http://localhost:8080".
  -s=*, --scheme                  Specifies the scheme to use, default is "http", ignored when --url is set.
  -h=*, --host                    Specifies the host to use, default is "localhost", ignored when --url is set.
  -P=*, --port                    Specifies the port to use, default is "8080", ignored when --url is set.
  -p=*, --path                    Specifies the path to use, default is "/json_rpc", ignored when --url is set.
  -m=*, --method                  Specifies the method to use.
  -j=*, --params                  Specifies the parameters to use (JSON object). I.e. '{"width": 1, "height": 1}', default is "{}".
  -J  , --json-output             Prints the request JSON instead of sending it.
  -U  , --url-output              Prints the request URL instead of sending a request.
  -C  , --command-output          Prints the request command instead of sending a request.
  -h,   --help                    Prints this help message.

Installation

Recommend installation is through vs:

mkdir -p "$HOME/.local/bin"
ln -s "$(vs add "https://gist.github.com/69d9cf77643c9bc89410487d7b1ba210.git")/jrpc" "$HOME/.local/bin/jrpc

or just clone it somewhere.

# Created by https://www.toptal.com/developers/gitignore/api/windows,macos,linux,vim
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,macos,linux,vim
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### macOS Patch ###
# iCloud generated files
*.icloud
### Vim ###
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
# Session
Session.vim
Sessionx.vim
# Temporary
.netrwhist
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# End of https://www.toptal.com/developers/gitignore/api/windows,macos,linux,vim
#!/usr/bin/env sh
version="0.0.1"
sub() { sed "s/^/| /"; }
usage="$(cat << EOF
Usage: $(basename "$0") [OPTIONS]
Simple CLI tool for testing output from JSON RPC 2.0 services.
OPTIONS
-V, --version Prints the version of this script.
-v, --verbose Prints verbose information.
-u=*, --url Specifies the URL to use, default is "http://localhost:8080".
-s=*, --scheme Specifies the scheme to use, default is "http", ignored when --url is set.
-h=*, --host Specifies the host to use, default is "localhost", ignored when --url is set.
-P=*, --port Specifies the port to use, default is "8080", ignored when --url is set.
-p=*, --path Specifies the path to use, default is "/json_rpc", ignored when --url is set.
-m=*, --method Specifies the method to use.
-j=*, --params Specifies the parameters to use (JSON object). I.e. '{"width": 1, "height": 1}', default is "{}".
-J , --json-output Prints the request JSON instead of sending it.
-U , --url-output Prints the request URL instead of sending a request.
-C , --command-output Prints the request command instead of sending a request.
-h, --help Prints this help message.
EOF
)"
scheme="http"
host="localhost"
port="8080"
path="/json_rpc"
params="{}"
while [ "$#" -gt 0 ]; do
case "$1" in
-V|--version) echo "$version" && exit 0 ;;
-v|--verbose) verbose=1 ;;
-u=*|--url=*) url="${1#*=}" ;;
-s=*|--scheme=*) scheme="${1#*=}" ;;
-h=*|--host=*) host="${1#*=}" ;;
-P=*|--port=*) port="${1#*=}" ;;
-p=*|--path=*) path="${1#*=}" ;;
-m=*|--method=*) method="${1#*=}" ;;
-j=*|--params=*) params="${1#*=}" ;;
-J|--json-output) json_output=1 ;;
-U|--url-output) url_output=1 ;;
-C|--command-output) command_output=1 ;;
-h|--help) echo "$usage" && exit 129 ;;
*) echo Invalid command line flag "$1". >&2; exit 1 ;;
esac
shift
done
if [ "$method" = "" ]; then
echo No method specified. >&2
exit 1
fi
test "$verbose" && set -x
set -e
if [ "$url" = "" ]; then
url="$scheme://$host:$port$path"
fi
req='{"jsonrpc":"2.0","id":"0","method":"'"$method"'","params":'$params'}'
if [ "$url_output" ] || [ "$json_output" ] || [ "$command_output" ]; then
if [ "$url_output" ]; then
echo "$url"
fi
if [ "$json_output" ]; then
echo "$req"
fi
if [ "$command_output" ]; then
echo "curl "'"'"$url"'"'" -d '$req' -H 'Content-Type: application/json' --http0.9 --output -"
fi
exit 0
elif command -v "curl" >/dev/null 2>&1; then
curl "$url" -d "$req" -H 'Content-Type: application/json' --http0.9 --output -
echo ''
elif command -v "wget" >/dev/null 2>&1; then
wget -qO- --post-data "$req" --header 'Content-Type: application/json' "$url"
echo ''
else
echo "No curl or wget found." >&2
exit 1
fi
set +e && set +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment