ruby net / http put方法跟随重定向

时间:2015-01-29 16:15:25

标签: ruby http redirect put nest-api

我正在使用Nest恒温器api,它向api.nest.com发送一个put请求,然后重定向到firebase,这是一个后端作为他们构建平台的服务。我可以让这个重定向工作与卷曲,但不是红宝石网/ http.if有人可以帮助我会非常感激。

工作卷曲输出

 $ curl -v -L -X PUT "https://developer-api.nest.com/devices/thermostats/F_z36lno7kg_XJEL96C7wXx63kFCmZ22z?auth=ERASED" -H         "Content-Type: application/json" -d  '{"target_temperature_f": 72 }'
* Hostname was NOT found in DNS cache
*   Trying 184.73.153.254...
* Connected to developer-api.nest.com (184.73.153.254) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
* Server certificate: developer-api.nest.com
* Server certificate: Go Daddy Secure Certificate Authority - G2
* Server certificate: Go Daddy Root Certificate Authority - G2
 > PUT /devices/thermostats/F_z36lno7kg_XJEL96C7wXx63kFCmZ22z?auth=ERASED      HTTP/1.1
 > User-Agent: curl/7.37.1
 > Host: developer-api.nest.com
 > Accept: */*
 > Content-Type: application/json
 > Content-Length: 29
 >
* upload completely sent off: 29 out of 29 bytes
 < HTTP/1.1 307 Temporary Redirect
 < Content-Type: application/json; charset=UTF-8
 < Access-Control-Allow-Origin: *
 < Cache-Control: private, no-cache, max-age=0
 < Location: https://firebase-apiserver01-tah01-     iad01.dapi.production.nest.com:9553/devices/thermostats/F_z36lno7kg_XJEL96C7wXx63kFCmZ22z?auth=ERASED
 < Connection: close
 < Content-Length: 0
 <
* Closing connection 0
* Issue another request to this URL: 'https://firebase-apiserver01-tah01-     iad01.dapi.production.nest.com:9553/devices/thermostats/F_z36lno7kg_XJEL96C7wXx63kFCmZ22z?auth=ERASED'
* Hostname was NOT found in DNS cache
*   Trying 54.196.205.148...
* Connected to firebase-apiserver01-tah01-iad01.dapi.production.nest.com     (54.196.205.148) port 9553 (#1)
* TLS 1.2 connection using TLS_DHE_RSA_WITH_AES_128_CBC_SHA
* Server certificate: *.dapi.production.nest.com
* Server certificate: Go Daddy Secure Certificate Authority - G2
* Server certificate: Go Daddy Root Certificate Authority - G2
> PUT /devices/thermostats/F_z36lno7kg_JEL96C7wXx63kFCmZ22z?auth=ERASED HTTP/1.1
> User-Agent: curl/7.37.1
> Host: firebase-apiserver01-tah01-iad01.dapi.production.nest.com:9553
> Accept: */*
> Content-Type: application/json
> Content-Length: 29
>
* upload completely sent off: 29 out of 29 bytes
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8     < Access-Control-Allow-Origin: *
< Cache-Control: private, no-cache, max-age=0
< Connection: close
< Content-Length: 29
<
* Closing connection 1

非工作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>'

0 个答案:

没有答案