As we can see from Bitcoin Core code, the cookie file specifies a HTTP Basic Auth user named __cookie__ and a random password that changes after each restart of Bitcoin Core.
It is also explained quite well here on StackOverflow: __cookie__ is the basic auth username and abc123 is a randomly generated password.
Therefore, it's enough to add those values to a normal HTTP request.
A MVP example using cURL would be (replacing xxxxx with actual content from the .cookie file):
curl --user __cookie__:xxxxx -X POST -H 'Content-Type: application/json' -H 'X-Auth-Token: undefined' --data-raw '{"id":1, "jsonrpc":"2.0", "method":"getblockchaininfo"}' http://127.0.0.1:8332
It correctly returns:
{
"result": {
"chain": "main",
"blocks": 729713,
"headers": 729713,
"bestblockhash": "000000000000000000000fbfac1a91cdeaf64d689f7673d02613da9d10bfb284",
"difficulty": 27452707696466.39,
"mediantime": 1648651391,
"verificationprogress": 0.9999999227202188,
"initialblockdownload": false,
"chainwork": "00000000000000000000000000000000000000002b2a9f5f3ef98696e155655c",
"size_on_disk": 452426899872,
"pruned": false,
"softforks": {
"bip34": {
"type": "buried",
"active": true,
"height": 227931
},
"bip66": {
"type": "buried",
"active": true,
"height": 363725
},
"bip65": {
"type": "buried",
"active": true,
"height": 388381
},
"csv": {
"type": "buried",
"active": true,
"height": 419328
},
"segwit": {
"type": "buried",
"active": true,
"height": 481824
},
"taproot": {
"type": "bip9",
"bip9": {
"status": "active",
"start_time": 1619222400,
"timeout": 1628640000,
"since": 709632,
"min_activation_height": 709632
},
"height": 709632,
"active": true
}
},
"warnings": ""
},
"error": null,
"id": 1
}
Working Python3 code:
import requests, json
rpcPort = 8332
rpcUser = '__cookie__'
rpcPassword = 'xxxxx'
serverURL = 'http://' + rpcUser + ':' + rpcPassword + '@localhost:' + str(rpcPort)
headers = {'Content-Type': 'application/json'}
payload = json.dumps({'id': 1, 'method': 'getblockchaininfo', 'jsonrpc': '2.0'})
print(f'serverURL: {serverURL}')
print(f'payload: {payload}')
try:
response = requests.post(serverURL, headers=headers, data=payload)
print(response)
print(response.status_code)
print(response.reason)
print(response.text)
except requests.exceptions.HTTPError as e:
print(e)
print(e.status_code)
print(e.reason)
My C++ version which also works fine and just requires that httplib.h file to be downloaded and placed in the same folder:
#include "./httplib.h"
int main() {
int rpcPort = 8332;
const char* rpcUser = "__cookie__";
const char* rpcPassword = "8abb90c7902e13105976a94ff5abbc4c9526f745235438bccc8b23aa2d9df0b3";
std::string serverURL = "127.0.0.1";
std::string body = "{\"id\":1, \"method\":\"getblockchaininfo\", \"jsonrpc\":\"2.0\"}";
httplib::Client cli(serverURL, rpcPort);
cli.set_basic_auth(rpcUser, rpcPassword);
if (auto res = cli.Post("/", body, "application/json")) {
std::cout << res->status << std::endl;
std::cout << res->body << std::endl;
}
else {
auto err = res.error();
std::cout << "Error: " << err << std::endl;
}
}
$~/test/cppbitcointest> g++ -l:libcrypto.so.1.1 bitcointest.cpp -o bitcointest && ./bitcointest
200
{"result":{"chain":"main","blocks":729726,"headers":729726,"bestblockhash":"00000000000000000007f5b05ab0ce3816fcf0c6593b3aa11dc062d4ecf3142e","difficulty":27452707696466.39,"mediantime":1648656001,"verificationprogress":0.9999907234912998,"initialblockdownload":false,"chainwork":"00000000000000000000000000000000000000002b2be3f65323c1f032368092","size_on_disk":452447125068,"pruned":false,"softforks":{"bip34":{"type":"buried","active":true,"height":227931},"bip66":{"type":"buried","active":true,"height":363725},"bip65":{"type":"buried","active":true,"height":388381},"csv":{"type":"buried","active":true,"height":419328},"segwit":{"type":"buried","active":true,"height":481824},"taproot":{"type":"bip9","bip9":{"status":"active","start_time":1619222400,"timeout":1628640000,"since":709632,"min_activation_height":709632},"height":709632,"active":true}},"warnings":""},"error":null,"id":1}
I believe the RPC you're looking for is listunspent.
By replacing the body in the above code examples with the following, it should return what you're looking for.
'{"jsonrpc": "1.0", "id": "curltest", "method": "listunspent", "params": [6, 9999999]}'