I am trying RPC request to bitcoind node,using Kotlin. My code looks like

import khttp.delete as httpDelete
val node_url = "http://rpcusr:rpcpass@"
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("")))

and its output -->

<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<BODY><H1>401 Unauthorized.</H1></BODY>

bitcoin.conf -->


Using curl i am getting desired output

CURL command-->

curl -v --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockchaininfo", "params": [] }' -H 'content-type: text/plain;' http://abc:pass@

o/p -->


The reason why i am asking this question on this forum is, i took the same approach to get rpc response from Ethereum node and i am getting proper response,the error is occuring only with bitcoin & litecoin node

Any clue why it is behaving this way ?

  • mapOf("Contenet-Type" to "application.json"),. Try changing that to Content-Type. If it still doesn't work, try application/json as well. – Raghav Sood Jun 19 '18 at 20:08
  • didnt work with any of the combination . Response is same in all cases – cryptoKTM Jun 19 '18 at 20:53
  • Can you post the relevant bitcoind debug.log output? That may have some clues – chytrik Jun 20 '18 at 20:31
  • debug.log only shows blocks being downloaded and nothing relevant – cryptoKTM Jun 21 '18 at 7:16

You are not properly sending the authentication headers. From a quick reading of the documentation, you need to add auth=BasicAuthorization("rpcuser", "rpcpass") to your post request because the URL http://rpcusr:rpcpass@" is not sufficient for khttp to send the basic authentication headers.


We need to pass Authorization method as :

val jsonstring = khttp.post(
        url = node_url,
        headers = mapOf("Authorization" to computeBasicAuth("abc","pass")),
        data = JSONObject("{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", \"method\": \"getrawmempool\", \"params\": [] }")


fun computeBasicAuth(user: String, password: String) ="Basic ${BASE64.encodeToString("$user:$password".toByteArray())}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.