heroku上的Sinatra应用程序使用GET和查询参数提供400个BAD_REQUEST错误

时间:2014-06-02 21:47:10

标签: ruby http heroku sinatra

我有一个简单的sinatra应用程序,它应该接收使用查询参数的GET请求,并获取这些参数并将它们转储到txt文件中。

当我使用telnet在本地测试时 - 一切都很棒。我收到了200条回复。第二个我把它全部放在heroku上,没有骰子。一切都有400 BAD_REQUESTS。当我使用浏览器时,一切都很好。我只是不能使用telnet(或我的arduino,这是我将使用的)发送。

直播网站: http://weathervane.herokuapp.com/

示例查询字符串: /sensor?temp=100&hum=200

此外,除了HTTP错误之外,看起来第二个参数(hum)也会被切断: http://cl.ly/image/3P46382p0D0v

这是索引路径

get '/' do
  erb :index
end

这是/传感器路线。

get '/sensor' do

  File.open 'temp_data.txt', 'w' do |f|
    f.write params[:temp]
  end

  File.open 'hum_data.txt', 'w' do |f|
    f.write params[:hum]
  end

  "OK, Temp: #{params[:temp]} & Hum: #{params[:hum]}" 

end

同样,这一切都在本地工作......我只是错过了我的heroku设置中的一步吗?

更新:以下是我尝试获取索引和传感器路径时的telnet打印输出。这只是因为我没有在请求中添加host:头。

标头中没有主机的索引。

telnet weathervane.herokuapp.com 80
Trying 50.17.239.217...
Connected to us-east-1-a.route.herokuapp.com.
Escape character is '^]'.
GET / HTTP/1.1

HTTP/1.1 400 BAD_REQUEST
Content-Length: 0
Connection: Close

Connection closed by foreign host.

标题中包含主机的索引。 (看起来它就行了!)

telnet weathervane.herokuapp.com 80
Trying 23.23.214.121...
Connected to us-east-1-a.route.herokuapp.com.
Escape character is '^]'.
GET / HTTP/1.1
Host: weathervane.herokuapp.com  

HTTP/1.1 200 OK 
Content-Type: text/html;charset=utf-8
Date: Tue, 03 Jun 2014 00:17:03 GMT
Server: WEBrick/1.3.1 (Ruby/2.0.0/2014-05-08)
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
Content-Length: 901
Connection: keep-alive

<!DOCTYPE html>
<html>

  <head>
    <script src="jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>Compost Monitor - aka "weathervane"</title>
  </head>

  <body>

    <h1>Hi there!</h1>

    <div class="data">
      <div class="dataTitle">Time (at server):</div>
      <div id="timeDisplay">Getting time....</div>
    </div>

    <div class="data">
      <div class="dataTitle">Pile Tempurature:</div>
      <div id="tempDisplay">Getting temperature...</div>
    </div>

    <div class="data">
      <div class="dataTitle">Pile Humidity:</div>
      <div id="humDisplay">Getting humidity...</div>
    </div>

  <script type="text/javascript">
    setInterval(function(){
      $("#timeDisplay").load("/time");
      $("#tempDisplay").load("/sensor/temp");
      $("#humDisplay").load("/sensor/hum");
    }, 5000);
  </script>

  </body>

</html>

这是没有主机头的/ sensor路由。

telnet weathervane.herokuapp.com 80
Trying 54.225.145.230...
Connected to us-east-1-a.route.herokuapp.com.
Escape character is '^]'.
GET /sensor?temp=100&hum=200 
HTTP/1.1 400 BAD_REQUEST
Content-Length: 0
Connection: Close

Connection closed by foreign host.

这是带有主机头的/ sensor路由。看看可爱的200 ok状态。 :)

telnet weathervane.herokuapp.com 80
Trying 54.225.77.87...
Connected to us-east-1-a.route.herokuapp.com.
Escape character is '^]'.
GET /sensor?temp=100&hum=200 HTTP/1.1
Host: weathervane.herokuapp.com

HTTP/1.1 200 OK 
Content-Type: text/html;charset=utf-8
Date: Tue, 03 Jun 2014 00:20:48 GMT
Server: WEBrick/1.3.1 (Ruby/2.0.0/2014-05-08)
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
Content-Length: 24
Connection: keep-alive

OK, Temp: 100 & Hum: 200

0 个答案:

没有答案