<p>We need to pass Authorization method as :</p>

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

<p>where:</p>

<pre><code>fun computeBasicAuth(user: String, password: String) ="Basic ${BASE64.encodeToString("$user:$password".toByteArray())}"
</code></pre>

CC BY-SA 4.0
2018-06-24T16:10:04.877Z
2018-06-24T16:10:04.877Z
Parishilan Rayamajhi
<p>You are not properly sending the authentication headers. From a quick reading of the documentation, you need to add <code>auth=BasicAuthorization("rpcuser", "rpcpass")</code> to your post request because the URL <code>http://rpcusr:rpcpass@127.0.0.1:18443"</code> is not sufficient for khttp to send the basic authentication headers.</p>

CC BY-SA 4.0
2018-06-20T22:58:56.783Z
2018-06-20T22:58:56.783Z
Ava Chow
<p>I am trying RPC request to bitcoind node,using  <strong>Kotlin</strong>. My code looks like</p>

<pre><code>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)
</code></pre>

<p>and its output --></p>

<pre><code>&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Error&lt;/TITLE&gt;
&lt;META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;&lt;H1&gt;401 Unauthorized.&lt;/H1&gt;&lt;/BODY&gt;
&lt;/HTML&gt;
</code></pre>

<p><strong>bitcoin.conf</strong> --></p>

<pre><code>rpcuser=rpcusr
rpcpassword=rpcpass
rpcallowip=0.0.0.0/0
server=1
</code></pre>

<p>Using curl i am getting desired output</p>

<p><strong>CURL  command--></strong></p>

<pre><code>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
</code></pre>

<p><strong>o/p</strong> --></p>

<pre><code>{"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"}
</code></pre>

<blockquote>
  <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 &amp; litecoin node</p>
</blockquote>

<p>Any clue why it is behaving this way ?</p>

CC BY-SA 4.0
2018-06-19T20:08:53.770Z
`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
CC BY-SA 4.0
2018-06-19T20:53:33.313Z
didnt work with any of the combination . Response is same in all cases
Parishilan Rayamajhi
CC BY-SA 4.0
2018-06-20T20:31:17.703Z
Can you post the relevant bitcoind debug.log output? That may have some clues
chytrik
CC BY-SA 4.0
2018-06-21T07:16:25.890Z
debug.log only shows blocks being downloaded and nothing relevant
Parishilan Rayamajhi
<bitcoin-core><json-rpc>
RPC call to bitcoin node on local machine respond back with code: 401
Parishilan Rayamajhi
# RPC call to bitcoin node on local machine respond back with code: 401

(Asked by: username_0 on 2018-06-19)

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@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)

```

and its output -->

```
<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

```

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@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"}

```

> 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 ?

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

> **Comments:**
> **username_1**: `mapOf("Contenet-Type" to "application.json"),`. Try changing that to `Content-Type`. If it still doesn't work, try `application/json` as well.  
> **username_0**: didnt work with any of the combination . Response is same in all cases  
> **username_2**: Can you post the relevant bitcoind debug.log output? That may have some clues  
> **username_0**: debug.log only shows blocks being downloaded and nothing relevant  

---

## 2 answers

### Answer by username_0 on 2018-06-24. Score: 1 | Accepted answer

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\": [] }")
)

```

where:

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

```


---

### Answer by username_3 on 2018-06-20. Score: 1

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@127.0.0.1:18443"` is not sufficient for khttp to send the basic authentication headers.


---

