<p>From the provided logs, it seems that your code is reaching the point where it sends the sendtoaddress RPC request to the Bitcoin node. However, the issue might be related to the response handling part of your code, which is not included in the snippet you provided.</p>
<p>You should continue the logs and inspect the response received from the Bitcoin node. Check the status code, and if it's not in the 2xx range, it could indicate an issue with the request. Additionally, parse and analyze the response body to get more information about the problem.</p>
<p>Here are a few steps to help you debug:</p>
<p>After resp, err := client.Do(req), check the HTTP status code:
if resp.StatusCode &lt; 200 || resp.StatusCode &gt;= 300 {
log.Errorf(&quot;Non-successful HTTP response: %v&quot;, resp.Status)
// Handle the error appropriately
}</p>
<p>Read and parse the response body:
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf(&quot;Failed to read response body: %v&quot;, err)
// Handle the error appropriately
}</p>
<p>log.Printf(&quot;Response Body: %s&quot;, body)</p>
<p>This should give you more information about what might be going wrong with the sendtoaddress RPC call. Keep an eye out for any error messages or additional details in the response body that can help diagnose the issue.</p>

CC BY-SA 4.0
2024-02-26T15:12:44.310Z
2024-02-26T15:12:44.310Z
Jake Stanton
<p>The steps i followed are i first load the wallet in a different method which works, then i check the balance of the wallet in a different method which works, and then I call this sendbitcoin method(below).  I can see that the bitcoind server gets the message but it returns with a 500.</p>
<p>If i use bitcoin-cli it works however i need to add a -rpcwallet flag.  It's my understanding that since the wallet has been loaded i shouldn't need to use flag.  My wallets aren't encrypted so i don't believe i need to use a passphrase.</p>
<p>My bitcoin.conf file looks like this.</p>
<pre><code>rpcuser=rpcuser
rpcpassword=rpcpassword
testnet=1
prune=550
server=1
dbcache=1000
[test]
debug=mempool
debug=rpc
dbcache=1000
rpcport=8332
rpcbind=0.0.0.0
rpcallowip=172.19.0.0/16
minconf=1
fallbackfee=0.00000001
</code></pre>
<p>And the method looks like.</p>
<pre><code>func (w *Wallet) SendBitcoin(sendTo string, amount float64, coachID uint) (BitcoinTransaction, error) {

username := os.Getenv(&quot;RPC_USER&quot;)
password := os.Getenv(&quot;RPC_PASSWORD&quot;)
rpcURL := os.Getenv(&quot;BITCOIN_NODE_HOST&quot;)

if username == &quot;&quot; || password == &quot;&quot; || rpcURL == &quot;&quot; {
    log.Error(&quot;RPC_USER, RPC_PASSWORD, or BITCOIN_NODE_HOST environment variables are not set&quot;)
    return BitcoinTransaction{}, errors.New(&quot;RPC_USER, RPC_PASSWORD, or BITCOIN_NODE_HOST environment variables are not set&quot;)
}

// Prepare the RPC request payload
requestBody := map[string]interface{}{
    &quot;jsonrpc&quot;: &quot;1.0&quot;,
    &quot;method&quot;:  &quot;sendtoaddress&quot;,
    &quot;params&quot;:  []interface{}{sendTo, amount},
    &quot;id&quot;:      1,
}

logrus.Printf(&quot;request body : %v&quot;, requestBody)
logrus.Printf(&quot;Sending on wallet %v&quot;, w.Name)

jsonData, err := json.Marshal(requestBody)
if err != nil {
    log.Errorf(&quot;failed to marshal JSON request: %v&quot;, err)
    return BitcoinTransaction{}, fmt.Errorf(&quot;failed to marshal JSON request: %v&quot;, err)
}

logrus.Printf(&quot;JSON body : %v&quot;, jsonData)

client := &amp;http.Client{}

req, err := http.NewRequest(&quot;POST&quot;, rpcURL, bytes.NewBuffer(jsonData))
if err != nil {
    log.Errorf(&quot;failed to create HTTP request: %v&quot;, err)
    return BitcoinTransaction{}, fmt.Errorf(&quot;failed to create HTTP request: %v&quot;, err)
}

req.SetBasicAuth(username, password)
req.Header.Set(&quot;Content-Type&quot;, &quot;application/json&quot;)

// Send HTTP request
resp, err := client.Do(req)
if err != nil {
    log.Errorf(&quot;failed to send HTTP request: %v&quot;, err)
    return BitcoinTransaction{}, fmt.Errorf(&quot;failed to send HTTP request: %v&quot;, err)
}
//...
</code></pre>
<p>logs</p>
<pre><code>[00] bitcoin      | 2024-02-22T22:36:59Z [rpc] ThreadRPCServer 
 method=loadwallet user=rpcuser
[00] rest-server  | time=&quot;2024-02-22T22:36:59Z&quot; level=info msg=&quot;balance 
= 0.0003933699999999999, amount = 0.0001&quot;
[00] bitcoin      | 2024-02-22T22:36:59Z Using SQLite Version 3.38.5
[00] bitcoin      | 2024-02-22T22:36:59Z Using wallet 
 /home/bitcoin/.bitcoin/testnet3/wallets/111-20240221
[00] bitcoin      | 2024-02-22T22:36:59Z [rpc] ThreadRPCServer 
method=sendtoaddress user=rpcuser
[00] rest-server  | time=&quot;2024-02-22T22:36:59Z&quot; level=info msg=&quot;request 
 body : map[id:1 jsonrpc:1.0 method:sendtoaddress params: 
[tb1qljunwv4hv8nx3mwsrup4fse5zhahyjtmx0fwzl 0.0001 ]]&quot;
[00] rest-server  | time=&quot;2024-02-22T22:37:00Z&quot; level=info 
msg=&quot;unexpected status code: 500&quot;
</code></pre>

CC BY-SA 4.0
2024-02-23T07:02:49.967Z
Can you see the error message from the Bitcoin Core debug.log? Log file can be found in your Bitcoin datadir, for example in Linux the testnet logs are in `~/.bitcoin/testnet/debug.log`
Juha Siivikko
<bitcoin-core><json-rpc><sendtoaddress>
Using sendtoaddress is failing using json-rpc and I don't know why
Tim Lieberman
# Using sendtoaddress is failing using json-rpc and I don't know why

(Asked by: username_0 on 2024-02-22)

The steps i followed are i first load the wallet in a different method which works, then i check the balance of the wallet in a different method which works, and then I call this sendbitcoin method(below).  I can see that the bitcoind server gets the message but it returns with a 500.
If i use bitcoin-cli it works however i need to add a -rpcwallet flag.  It's my understanding that since the wallet has been loaded i shouldn't need to use flag.  My wallets aren't encrypted so i don't believe i need to use a passphrase.
My bitcoin.conf file looks like this.
rpcuser=rpcuser
rpcpassword=rpcpassword
testnet=1
prune=550
server=1
dbcache=1000
[test]
debug=mempool
debug=rpc
dbcache=1000
rpcport=8332
rpcbind=0.0.0.0
rpcallowip=172.19.0.0/16
minconf=1
fallbackfee=0.00000001

And the method looks like.
func (w *Wallet) SendBitcoin(sendTo string, amount float64, coachID uint) (BitcoinTransaction, error) {

username := os.Getenv("RPC_USER")
password := os.Getenv("RPC_PASSWORD")
rpcURL := os.Getenv("BITCOIN_NODE_HOST")

if username == "" || password == "" || rpcURL == "" {
    log.Error("RPC_USER, RPC_PASSWORD, or BITCOIN_NODE_HOST environment variables are not set")
    return BitcoinTransaction{}, errors.New("RPC_USER, RPC_PASSWORD, or BITCOIN_NODE_HOST environment variables are not set")
}

// Prepare the RPC request payload
requestBody := map[string]interface{}{
    "jsonrpc": "1.0",
    "method":  "sendtoaddress",
    "params":  []interface{}{sendTo, amount},
    "id":      1,
}

logrus.Printf("request body : %v", requestBody)
logrus.Printf("Sending on wallet %v", w.Name)

jsonData, err := json.Marshal(requestBody)
if err != nil {
    log.Errorf("failed to marshal JSON request: %v", err)
    return BitcoinTransaction{}, fmt.Errorf("failed to marshal JSON request: %v", err)
}

logrus.Printf("JSON body : %v", jsonData)

client := &http.Client{}

req, err := http.NewRequest("POST", rpcURL, bytes.NewBuffer(jsonData))
if err != nil {
    log.Errorf("failed to create HTTP request: %v", err)
    return BitcoinTransaction{}, fmt.Errorf("failed to create HTTP request: %v", err)
}

req.SetBasicAuth(username, password)
req.Header.Set("Content-Type", "application/json")

// Send HTTP request
resp, err := client.Do(req)
if err != nil {
    log.Errorf("failed to send HTTP request: %v", err)
    return BitcoinTransaction{}, fmt.Errorf("failed to send HTTP request: %v", err)
}
//...

logs
[00] bitcoin      | 2024-02-22T22:36:59Z [rpc] ThreadRPCServer 
 method=loadwallet user=rpcuser
[00] rest-server  | time="2024-02-22T22:36:59Z" level=info msg="balance 
= 0.0003933699999999999, amount = 0.0001"
[00] bitcoin      | 2024-02-22T22:36:59Z Using SQLite Version 3.38.5
[00] bitcoin      | 2024-02-22T22:36:59Z Using wallet 
 /home/bitcoin/.bitcoin/testnet3/wallets/111-20240221
[00] bitcoin      | 2024-02-22T22:36:59Z [rpc] ThreadRPCServer 
method=sendtoaddress user=rpcuser
[00] rest-server  | time="2024-02-22T22:36:59Z" level=info msg="request 
 body : map[id:1 jsonrpc:1.0 method:sendtoaddress params: 
[tb1qljunwv4hv8nx3mwsrup4fse5zhahyjtmx0fwzl 0.0001 ]]"
[00] rest-server  | time="2024-02-22T22:37:00Z" level=info 
msg="unexpected status code: 500"

**Tags:** bitcoin-core, json-rpc, sendtoaddress

> **Comments:**
> **username_1**: Can you see the error message from the Bitcoin Core debug.log? Log file can be found in your Bitcoin datadir, for example in Linux the testnet logs are in `~/.bitcoin/testnet/debug.log`  

---

## 1 answers

### Answer by username_2 on 2024-02-26. Score: -1

From the provided logs, it seems that your code is reaching the point where it sends the sendtoaddress RPC request to the Bitcoin node. However, the issue might be related to the response handling part of your code, which is not included in the snippet you provided.

You should continue the logs and inspect the response received from the Bitcoin node. Check the status code, and if it's not in the 2xx range, it could indicate an issue with the request. Additionally, parse and analyze the response body to get more information about the problem.

Here are a few steps to help you debug:

After resp, err := client.Do(req), check the HTTP status code:
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
log.Errorf("Non-successful HTTP response: %v", resp.Status)
// Handle the error appropriately
}

Read and parse the response body:
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("Failed to read response body: %v", err)
// Handle the error appropriately
}

log.Printf("Response Body: %s", body)

This should give you more information about what might be going wrong with the sendtoaddress RPC call. Keep an eye out for any error messages or additional details in the response body that can help diagnose the issue.


---

