1 // Copyright (c) 2014 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
11 "github.com/btcsuite/btcd/btcjson"
14 // This example demonstrates how to create and marshal a command into a JSON-RPC
16 func ExampleMarshalCmd() {
17 // Create a new getblock command. Notice the nil parameter indicates
18 // to use the default parameter for that fields. This is a common
19 // pattern used in all of the New<Foo>Cmd functions in this package for
20 // optional fields. Also, notice the call to btcjson.Bool which is a
21 // convenience function for creating a pointer out of a primitive for
22 // optional parameters.
23 blockHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
24 gbCmd := btcjson.NewGetBlockCmd(blockHash, btcjson.Bool(false), nil)
26 // Marshal the command to the format suitable for sending to the RPC
27 // server. Typically the client would increment the id here which is
28 // request so the response can be identified.
30 marshalledBytes, err := btcjson.MarshalCmd(id, gbCmd)
36 // Display the marshalled command. Ordinarily this would be sent across
37 // the wire to the RPC server, but for this example, just display it.
38 fmt.Printf("%s\n", marshalledBytes)
41 // {"jsonrpc":"1.0","method":"getblock","params":["000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",false],"id":1}
44 // This example demonstrates how to unmarshal a JSON-RPC request and then
45 // unmarshal the concrete request into a concrete command.
46 func ExampleUnmarshalCmd() {
47 // Ordinarily this would be read from the wire, but for this example,
48 // it is hard coded here for clarity.
49 data := []byte(`{"jsonrpc":"1.0","method":"getblock","params":["000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",false],"id":1}`)
51 // Unmarshal the raw bytes from the wire into a JSON-RPC request.
52 var request btcjson.Request
53 if err := json.Unmarshal(data, &request); err != nil {
58 // Typically there isn't any need to examine the request fields directly
59 // like this as the caller already knows what response to expect based
60 // on the command it sent. However, this is done here to demonstrate
61 // why the unmarshal process is two steps.
62 if request.ID == nil {
63 fmt.Println("Unexpected notification")
66 if request.Method != "getblock" {
67 fmt.Println("Unexpected method")
71 // Unmarshal the request into a concrete command.
72 cmd, err := btcjson.UnmarshalCmd(&request)
78 // Type assert the command to the appropriate type.
79 gbCmd, ok := cmd.(*btcjson.GetBlockCmd)
81 fmt.Printf("Incorrect command type: %T\n", cmd)
85 // Display the fields in the concrete command.
86 fmt.Println("Hash:", gbCmd.Hash)
87 fmt.Println("Verbose:", *gbCmd.Verbose)
88 fmt.Println("VerboseTx:", *gbCmd.VerboseTx)
91 // Hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
96 // This example demonstrates how to marshal a JSON-RPC response.
97 func ExampleMarshalResponse() {
98 // Marshal a new JSON-RPC response. For example, this is a response
99 // to a getblockheight request.
100 marshalledBytes, err := btcjson.MarshalResponse(1, 350001, nil)
106 // Display the marshalled response. Ordinarily this would be sent
107 // across the wire to the RPC client, but for this example, just display
109 fmt.Printf("%s\n", marshalledBytes)
112 // {"result":350001,"error":null,"id":1}
115 // This example demonstrates how to unmarshal a JSON-RPC response and then
116 // unmarshal the result field in the response to a concrete type.
117 func Example_unmarshalResponse() {
118 // Ordinarily this would be read from the wire, but for this example,
119 // it is hard coded here for clarity. This is an example response to a
120 // getblockheight request.
121 data := []byte(`{"result":350001,"error":null,"id":1}`)
123 // Unmarshal the raw bytes from the wire into a JSON-RPC response.
124 var response btcjson.Response
125 if err := json.Unmarshal(data, &response); err != nil {
126 fmt.Println("Malformed JSON-RPC response:", err)
130 // Check the response for an error from the server. For example, the
131 // server might return an error if an invalid/unknown block hash is
133 if response.Error != nil {
134 fmt.Println(response.Error)
138 // Unmarshal the result into the expected type for the response.
139 var blockHeight int32
140 if err := json.Unmarshal(response.Result, &blockHeight); err != nil {
141 fmt.Printf("Unexpected result type: %T\n", response.Result)
144 fmt.Println("Block height:", blockHeight)
147 // Block height: 350001