Search code examples
crystal-lang

Crystal-lang httpget basic_auth


I am writing some code in Ruby... But I can't figure out how basic authentication works with Crystal-lang.

In ruby I had to always use request.basic_auth but that probably doesn't work in Crystal lang.
What am I doing ruby? Could someone write that line request.basic_auth in Crystal-lang?

    request = HTTP::Client.get "http://127.0.0.1:#{ports[coinName]}/"
    request.basic_auth username([coinName], password[coinName])
    json = JSON.parse(request.body

error

in ./src/coinHashRate.cr:29: undefined method 'basic_auth' for HTTP::Client::Response

    request.basic_auth username[coinName], password[coinName]
            ^~~~~~~~~~

Solution

  • You need to do:

    require "http/client"
    
    client = HTTP::Client.new "127.0.0.1", ports[coinName]
    client.basic_auth(username[coinName], password[coinName])
    client.get "/"
    

    You configure the client with basic auth, not the request nor the response.