Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby heroku上的Sinatra应用程序使用GET和查询参数给出400个错误请求_Ruby_Http_Heroku_Sinatra - Fatal编程技术网

Ruby heroku上的Sinatra应用程序使用GET和查询参数给出400个错误请求

Ruby heroku上的Sinatra应用程序使用GET和查询参数给出400个错误请求,ruby,http,heroku,sinatra,Ruby,Http,Heroku,Sinatra,我有一个简单的sinatra应用程序,它应该接收使用查询参数的GET请求,并将这些参数转储到txt文件中 当我使用telnet在本地测试它时,一切都很好。我收到200份回复。第二次我把一切都放在heroku身上,没有骰子。400个糟糕的请求。当我使用浏览器时,一切都很好。我只是无法使用telnet(或者我的arduino,这就是我将要使用的)发送 现场直播: 示例查询字符串: /sensor?温度=100&hum=200 此外,除了HTTP错误之外,第二个参数(hum)似乎也被切断了: 这是

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

当我使用telnet在本地测试它时,一切都很好。我收到200份回复。第二次我把一切都放在heroku身上,没有骰子。400个糟糕的请求。当我使用浏览器时,一切都很好。我只是无法使用telnet(或者我的arduino,这就是我将要使用的)发送

现场直播:

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

此外,除了HTTP错误之外,第二个参数(hum)似乎也被切断了:

这是索引路径

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:header

标题中没有主机的索引

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.
标题中包含主机的索引。(看起来它成功了!)

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


对我来说没问题。粘贴telnet会话的结果,以便我们查看发生了什么。需要考虑的几件事:(1)
&
字符在许多shell中表示“在后台运行”,您可能需要根据所做的操作引用请求行;(2) 您需要一个
主机:
头作为HTTP请求的一部分,否则Heroku将不知道该请求是针对您的应用程序的;(3) (与您当前的问题没有直接关系)Heroku使用了,因此您无法在那里安全地编写文件。谢谢Matt。我将编辑带有telnet回复的问题。我将尝试添加一个主机,看看这是否有帮助。完全理解短暂的问题。txt不是一个长期的解决方案。最后,我将学习如何为此创建数据库。:)这就成功了。它实际上只需要主机头。FML。谢谢你@matt我快疯了。
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>
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.
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