Ich versuche eine RPC-Anfrage an den Bitcoin-Knoten mit Kotlin . Mein Code sieht aus wie
import khttp.delete as httpDelete
val node_url = "http://rpcusr:rpcpass@127.0.0.1:18443"
val json_resp = khttp.post(url = node_url,
headers = mapOf("Content-Type" to "application/json"), //also tried application.json and text/plain
json = mapOf("id" to "curltext","jsonrpc" to 1,"method" to "getblockchaininfo","params" to arrayOf("")))
println(json_resp.text)
und seine Ausgabe -->
<HTML>
<HEAD>
<TITLE>Error</TITLE>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
</HEAD>
<BODY><H1>401 Unauthorized.</H1></BODY>
</HTML>
bitcoin.conf -->
rpcuser=rpcusr
rpcpassword=rpcpass
rpcallowip=0.0.0.0/0
server=1
Mit curl erhalte ich die gewünschte Ausgabe
CURL-Befehl-->
curl -v --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockchaininfo", "params": [] }' -H 'content-type: text/plain;' http://abc:pass@127.0.0.1:18443
o/p -->
{"result":{"chain":"regtest","blocks":0,"headers":0,"bestblockhash":"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206","difficulty":4.656542373906925e-10,"mediantime":1296688602,"verificationprogress":1,"initialblockdownload":true,"chainwork":"0000000000000000000000000000000000000000000000000000000000000002","size_on_disk":293,"pruned":false,"softforks":[{"id":"bip34","version":2,"reject":{"status":false}},{"id":"bip66","version":3,"reject":{"status":false}},{"id":"bip65","version":4,"reject":{"status":false}}],"bip9_softforks":{"csv":{"status":"defined","startTime":0,"timeout":9223372036854775807,"since":0},"segwit":{"status":"active","startTime":-1,"timeout":9223372036854775807,"since":0}},"warnings":""},"error":null,"id":"curltest"}
Der Grund, warum ich diese Frage in diesem Forum stelle, ist, dass ich den gleichen Ansatz gewählt habe, um eine RPC-Antwort vom Ethereum-Knoten zu erhalten, und ich erhalte die richtige Antwort, der Fehler tritt nur bei Bitcoin- und Litecoin-Knoten auf
Irgendeine Ahnung, warum es sich so verhält?
Sie senden die Authentifizierungsheader nicht ordnungsgemäß. Nach einem schnellen Lesen der Dokumentation müssen Sie auth=BasicAuthorization("rpcuser", "rpcpass")
Ihre Post-Anforderung ergänzen, da die URL http://rpcusr:rpcpass@127.0.0.1:18443"
für khttp nicht ausreicht, um die grundlegenden Authentifizierungsheader zu senden.
Wir müssen die Autorisierungsmethode wie folgt übergeben:
val jsonstring = khttp.post(
url = node_url,
headers = mapOf("Authorization" to computeBasicAuth("abc","pass")),
data = JSONObject("{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", \"method\": \"getrawmempool\", \"params\": [] }")
)
wo:
fun computeBasicAuth(user: String, password: String) ="Basic ${BASE64.encodeToString("$user:$password".toByteArray())}"
Raghav Sood
mapOf("Contenet-Type" to "application.json"),
. Versuchen Sie, das zu ändernContent-Type
. Wenn es immer noch nicht funktioniert, versuchen Sieapplication/json
es auch.cryptoKTM
Chytrik
cryptoKTM