Ruby 发送带有密钥和密码的请求

Ruby 发送带有密钥和密码的请求,ruby,json,get,Ruby,Json,Get,我打算使用net/http发送GET请求,如下所示: require 'net/http' response = Net::HTTP.get_response("example.com", "/whoareyou.json") 但是我需要在url中包含key和secret。我试着这样做: require 'net/http' response = Net::HTTP.get_response("<key>:<secret>@example.com", "/whoarey

我打算使用net/http发送GET请求,如下所示:

require 'net/http'
response = Net::HTTP.get_response("example.com", "/whoareyou.json")
但是我需要在url中包含key和secret。我试着这样做:

require 'net/http'
response = Net::HTTP.get_response("<key>:<secret>@example.com", "/whoareyou.json")

HTTP不支持从URL进行基本身份验证。您需要调用req.basic\u auth'user','pass'

我还建议您使用HTTP Party gem(),它可以真正简化大多数HTTP内容,例如,请参阅此答案,了解如何将basic auth与HTTP Party一起使用:

在您的示例中:

res = HTTParty.get("http:example.com/whoareyou.json", :basic_auth=> {:username =>user, :password => password})
                 :basic_auth => auth)
res = HTTParty.get("http:example.com/whoareyou.json", :basic_auth=> {:username =>user, :password => password})
                 :basic_auth => auth)