gpt4 book ai didi

ruby-on-rails - 尝试通过 Rails 访问 Basecamp API

转载 作者:太空宇宙 更新时间:2023-11-03 16:08:36 34 4
gpt4 key购买 nike

我试图通过 Rails 访问 Basecamp API,但它以 SocketError 响应。我的代码是这样的:

require 'rubygems'
require 'net/https'
http = Net::HTTP.new('https://webonise.basecamphq.com')
http.use_ssl = true
http.start do |http|
req = Net::HTTP::GET.new('/projects.xml')
req.basic_auth 'username' , 'password'
resp, data = http.request(req)
end

响应是:

SocketError: getaddrinfo: Name or service not known 

最佳答案

Net::HTTP.new 将主机名而非 URI 作为其第一个参数。尝试先调用 URI.parse 将 URI 分解为您想要的部分:

require 'rubygems'
require 'net/http'
uri = URI.parse("https://webonise.basecamphq.com/")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth 'username', 'password'
resp = http.request(req)
body = resp.body

您还必须从 body 方法的响应中获取正文。

关于ruby-on-rails - 尝试通过 Rails 访问 Basecamp API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8790198/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com