Unable to call an rpc method via curl with no authentication #10218

issue GildedHonour opened this issue on April 16, 2017
  1. GildedHonour commented at 5:12 AM on April 16, 2017: none

    My config: ~/.bitcoin/bitcoin.conf

    
      daemon=1
      prune=600
      maxconnections=12
      maxuploadtarget=20
      server=1
    
    

    Version:

    
    $ bitcoind --version
    Bitcoin Core Daemon version v0.14.0.0-g43a987c1c
    Copyright (C) 2009-2017 The Bitcoin Core developers
    

    Why doesn't this work and require authentication?

    $ curl -v --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/
    *   Trying 127.0.0.1...
    * Connected to 127.0.0.1 (127.0.0.1) port 8332 (#0)
    > POST / HTTP/1.1
    > Host: 127.0.0.1:8332
    > User-Agent: curl/7.47.0
    > Accept: */*
    > content-type: text/plain;
    > Content-Length: 71
    > 
    * upload completely sent off: 71 out of 71 bytes
    < HTTP/1.1 401 Unauthorized
    < WWW-Authenticate: Basic realm="jsonrpc"
    < Date: Sun, 16 Apr 2017 05:09:10 GMT
    < Content-Length: 0
    < Content-Type: text/html; charset=ISO-8859-1
    < 
    * Connection [#0](/bitcoin-bitcoin/0/) to host 127.0.0.1 left intact
    
    
  2. MarcoFalke commented at 9:32 AM on April 16, 2017: member

    Are you using a non-default value for -datadir?

  3. GildedHonour commented at 9:34 AM on April 16, 2017: none

    @MarcoFalke default one

  4. sipa commented at 9:42 AM on April 16, 2017: member

    You need to set a rpcuser/rpcpassword in bitcoin.conf, and pass that as authentication info in curl. If you don't specify one, bitcoind generates a password automatically and writes it into the authcookie file.

  5. GildedHonour commented at 9:45 AM on April 16, 2017: none

    @sipa ok, I want it to generate a password automatically and write it into the authcookie file, what should I do?

  6. sipa commented at 9:49 AM on April 16, 2017: member

    Then you should read the authcookie file yourself, and use the username/password combination in it when connecting. bitcoin-cli will do this automatically, but curl obviously can't do that.

  7. GildedHonour commented at 9:51 AM on April 16, 2017: none

    @sipa maybe you should read it? rpcuser/rpcpassword have become deprecated.

  8. MarcoFalke commented at 9:57 AM on April 16, 2017: member

    @GildedHonour Yes they are deprecated in favor of the new option rpcauth, which is the alternative to the default behavior of using a cookie file.

      -rpcauth=<userpw>
           Username and hashed password for JSON-RPC connections. The field
           <userpw> comes in the format: <USERNAME>:<SALT>$<HASH>. A
           canonical python script is included in share/rpcuser. The client
           then connects normally using the
           rpcuser=<USERNAME>/rpcpassword=<PASSWORD> pair of arguments. This
           option can be specified multiple times
    
  9. sipa commented at 9:58 AM on April 16, 2017: member

    Yes, you can use -rpcauth as well. But you should still use the username/password from the cookie file if you don't want to configure a pair yourself in bitcoin.conf.

  10. MarcoFalke commented at 10:00 AM on April 16, 2017: member

    @sipa I think the automatically generated cookie file is different from the human generated auth pair.

  11. GildedHonour commented at 10:02 AM on April 16, 2017: none

    @MarcoFalke I know. But authentication is an option which isn't mandatory. Hence, why doesn't my example work? @sipa the cookie file doesn't get created. What's its full name?

  12. sipa commented at 10:05 AM on April 16, 2017: member

    .cookie in the datadir

  13. sipa commented at 10:06 AM on April 16, 2017: member

    @GildedHonour Authentication is not optional. Configuring it yourself is optional, but bitcoind never accepts RPC connections without explicitly or implicitly configured username/password.

  14. GildedHonour commented at 10:08 AM on April 16, 2017: none

    @sipa ok, right. That file exists. However, my question remains.

  15. sipa commented at 10:13 AM on April 16, 2017: member

    I've answered it, I think. Bitcoind always requires authentication on connections.

  16. MarcoFalke commented at 10:13 AM on April 16, 2017: member

    I am pretty sure @sipa answered your question

    Q: Why doesn't this work and require authentication?

    A: Authentication is not optional.

  17. GildedHonour commented at 10:15 AM on April 16, 2017: none

    @sipa what did you mean by "Configuring it yourself is optional,"? How can I skip configuring it myself?

  18. sipa commented at 10:21 AM on April 16, 2017: member

    You have three options:

    • Set rpcuser and rpcpassword in bitcoin.conf. If you do that, you must pass that username and password as auth info in curl.
    • Set one or more rpcauth lines in bitcoin.conf. If you do that, you must pass one of those username/password pairs in curl.
    • Don't configure anything in bitcoin.conf. In that case, bitcoind will generate an authentication cookie for you, and write a username/password pair into the .cookie file. You must use the information in that file when connecting via curl.
  19. sipa closed this on Apr 16, 2017

  20. GildedHonour commented at 10:24 AM on April 16, 2017: none

    @sipa the 3rd isn't correct. This is what I have, no user name, no password:

    $ cat .bitcoin/.cookie 
    __cookie__:3784b8eb00726c09b275aff93882b7859cf482b8f714931343fds32431234gaa
    
    

    how can I use it?

  21. MarcoFalke commented at 10:27 AM on April 16, 2017: member

    @GildedHonour Use option two. You can type in ./share/rpcuser/rpcuser.py my_name and follow the instructions.

  22. sipa commented at 10:28 AM on April 16, 2017: member

    __cookie__ is the username. 3784b8eb00726c09b275aff93882b7859cf482b8f714931343fds32431234gaa is the password.

    Note that the 3rd method isn't really intended to be used that way. If you need extra work to support it from your client code, it's much easier to just configure a password with rpcpassword or rpcauth.

  23. GildedHonour commented at 10:29 AM on April 16, 2017: none

    thanks :)

  24. adilw3nomad commented at 2:50 PM on August 1, 2018: none

    @MarcoFalke I'm having a similar issue with a non-default data-dir option. Any help?

  25. MarcoFalke commented at 3:13 PM on August 1, 2018: member

    @adilw3nomad Make sure to pass the datadir to bitcoin-cli, so it can read the cookie or auth from the config file as well as the chain (main, test, regtest)

  26. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-02 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me