我试图复制此GET卷曲请求:
curl -D- -X GET -H "Authorization: Basic dGVzdEB0YXByZXNlYXJjaC5jb206NGMzMTg2Mjg4YWUyM2ZkOTY2MWNiNWRmY2NlMTkzMGU="
-H "Content-Type: application/json" http://staging.example.com/api/v1/campaigns
使用电子邮件+ api密钥在Ruby中生成auth:
auth = "Basic" + Base64::encode64("test@example.com:4c3186288ae23fd9661cb5dfcce1930e")
在Ruby / Rails中复制它的最佳方法是什么?
以下是我一直在尝试的事情:
auth = "Basic" + Base64::encode64("example@example.com@:4c3186288ae23fd9661cb5dfcce1930e")
uri = URI.parse("https://www.staging.example.com/api/v1/campaigns")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request.env['HTTP_AUTHORIZATION'] = auth
response = http.request(request)
puts response.body
基于我发现的其他一些SO和博客,但后来我得到了undefined method 'env' for #<Net::HTTP::Get GET>
。
我有什么想法可以设置,所以消息就像这样?
Authorization: Basic dGVzdEB0YXByZXNlYXJjaC5jb206NGMzMTg2Mjg4YWUyM2ZkOTY2MWNiNWRmY2NlMTkzMGU=
答案 0 :(得分:7)
request['Authorization'] = "Basic " + Base64::encode64("example@example.com@:4c3186288ae23fd9661cb5dfcce1930e")
答案 1 :(得分:2)
这是一种更简单的方法,使用request.basic_auth
来处理所有混乱的细节:
request = Net::HTTP::Post.new(uri)
request.basic_auth(username, password)
http = Net::HTTP.new(uri.host, uri.port)
response = http.request(request)