带有follow重定向的ruby-net/httpput方法

带有follow重定向的ruby-net/httpput方法,ruby,http,redirect,put,nest-api,Ruby,Http,Redirect,Put,Nest Api,我正在使用Nest恒温器api,它向api.Nest.com发送put请求,然后重定向到firebase,firebase是他们构建平台的后端服务。我可以将此重定向用于curl,但不能用于rubies net/http。如果有人能帮助我,我将不胜感激 工作旋度输出 非工作Ruby实现 这可能看起来有点奇怪,我一直在拼命地破解它 重定向似乎有效,但读取重定向的页面却无效 def fetch(uri, limit = 10) # You should choose better except

我正在使用Nest恒温器api,它向api.Nest.com发送put请求,然后重定向到firebase,firebase是他们构建平台的后端服务。我可以将此重定向用于curl,但不能用于rubies net/http。如果有人能帮助我,我将不胜感激

工作旋度输出

非工作Ruby实现

这可能看起来有点奇怪,我一直在拼命地破解它

重定向似乎有效,但读取重定向的页面却无效

def fetch(uri, limit = 10)
    # You should choose better exception.
    raise ArgumentError, 'HTTP redirect too deep' if limit == 0

    uri = URI(uri)

    # Initializes the request by setting the HTTP headers, query and body parameters and the URI path
    req = Net::HTTP::Put.new(uri.path + 'devices/thermostats/F_z36lno7kg_XJEL96C7wXx63kFCmZ22z?auth=ERASED')
    req.set_form_data("set_temperature_f" => 79)
    req['Content-type'] = 'application/json'

    # Makes a HTTP PUT request using SSL
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(req)
    end

    puts res['location']

  case res
  when Net::HTTPSuccess     then res
  when Net::HTTPRedirection then fetch(res['location'], limit - 1)
  else
    res.error!
  end

end

irb(main):412:0* fetch('https://developer-api.nest.com/')
https://firebase-apiserver01-tah01iad01.dapi.production.nest.com:9553/devices/thermostats/F_z36lno7kg_XJEL96C7wXx63kFCmZ22z?auth=ERASED

Net::HTTPServerException: 400 "Bad Request"
from /opt/chef/embedded/lib/ruby/1.9.1/net/http.rb:2633:in `error!'
from (irb):407:in `fetch'
from (irb):405:in `fetch'
from (irb):412
from /opt/chef/embedded/bin/irb:12:in `<main>'

您启用了SSL吗?我忘了如何让Net::HTTP使用SSL:是的,我做了,但还是不行。我最终只是直接调用重定向的url。
def fetch(uri, limit = 10)
    # You should choose better exception.
    raise ArgumentError, 'HTTP redirect too deep' if limit == 0

    uri = URI(uri)

    # Initializes the request by setting the HTTP headers, query and body parameters and the URI path
    req = Net::HTTP::Put.new(uri.path + 'devices/thermostats/F_z36lno7kg_XJEL96C7wXx63kFCmZ22z?auth=ERASED')
    req.set_form_data("set_temperature_f" => 79)
    req['Content-type'] = 'application/json'

    # Makes a HTTP PUT request using SSL
    res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(req)
    end

    puts res['location']

  case res
  when Net::HTTPSuccess     then res
  when Net::HTTPRedirection then fetch(res['location'], limit - 1)
  else
    res.error!
  end

end

irb(main):412:0* fetch('https://developer-api.nest.com/')
https://firebase-apiserver01-tah01iad01.dapi.production.nest.com:9553/devices/thermostats/F_z36lno7kg_XJEL96C7wXx63kFCmZ22z?auth=ERASED

Net::HTTPServerException: 400 "Bad Request"
from /opt/chef/embedded/lib/ruby/1.9.1/net/http.rb:2633:in `error!'
from (irb):407:in `fetch'
from (irb):405:in `fetch'
from (irb):412
from /opt/chef/embedded/bin/irb:12:in `<main>'