Crystal-lang httpget basic_auth

我在Ruby中编写了一些代码......但我无法弄清楚基本身份验证如何与Crystal-lang一起工作 .

在ruby中,我必须始终使用request.basic_auth,但这可能不适用于Crystal lang .
我在做什么红宝石?有人可以在Crystal-lang中写一行request.basic_auth吗?

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

错误

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

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

回答(2)

2 years ago

你需要这样做:

require "http/client"

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

您使用基本身份验证配置客户端,而不是请求和响应 .

2 years ago

它应该像ruby一样工作 . 你运行它时会出现任何错误吗?

Here is a link to the original source of the function for clarification

使用新函数而不是get .

request = HTTP::Client.new "http://127.0.0.1/", ports[coinName]
request.basic_auth username[coinName], password[coinName]
response = request.get "/"
json = JSON.parse response.body