Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Add -j/json option to dcrctl.
Browse files Browse the repository at this point in the history
This option lets dcrctl print the raw json it both
sends and receives for commands.

Inspired by the similar option in dcrtime.
  • Loading branch information
jcvernaleo committed Jun 15, 2017
1 parent 9285b49 commit 016915c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/dcrctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type config struct {
RPCPassword string `short:"P" long:"rpcpass" default-mask:"-" description:"RPC password"`
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
RPCCert string `short:"c" long:"rpccert" description:"RPC server certificate chain for validation"`
PrintJSON bool `short:"j" long:"json" description:"Print json messages sent and received"`
NoTLS bool `long:"notls" description:"Disable TLS"`
Proxy string `long:"proxy" description:"Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)"`
ProxyUser string `long:"proxyuser" description:"Username for proxy server"`
Expand Down
8 changes: 8 additions & 0 deletions cmd/dcrctl/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func sendPostRequest(marshalledJSON []byte, cfg *config) ([]byte, error) {
protocol = "https"
}
url := protocol + "://" + cfg.RPCServer
if cfg.PrintJSON {
fmt.Println(string(marshalledJSON))
}
bodyReader := bytes.NewReader(marshalledJSON)
httpRequest, err := http.NewRequest("POST", url, bodyReader)
if err != nil {
Expand Down Expand Up @@ -119,6 +122,11 @@ func sendPostRequest(marshalledJSON []byte, cfg *config) ([]byte, error) {
return nil, fmt.Errorf("%s", respBytes)
}

// If requested, print raw json response.
if cfg.PrintJSON {
fmt.Println(string(respBytes))
}

// Unmarshal the response.
var resp dcrjson.Response
if err := json.Unmarshal(respBytes, &resp); err != nil {
Expand Down

0 comments on commit 016915c

Please sign in to comment.