Crystal-lang httpget basic_auth

问题描述:

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

在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] 
      ^~~~~~~~~~ 

你需要做的:

require "http/client" 

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

您配置客户端与基本身份验证,而不是请求或响应。

它应该工作一样红宝石。你运行它时是否有错误?

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

使用新的功能,而不是获取的。

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 
+0

是的。更新我的问题。查看错误日志 – Romano

+0

检查更新后的代码是否有效? –

+0

只需跟进,您是否可以为未来用户选择一个答案,并回答此问题? –