代码之家  ›  专栏  ›  技术社区  ›  Justin Searls

在Rails中,如何记录每个传入HTTP请求的全部内容?

  •  1
  • Justin Searls  · 技术社区  · 15 年前

    我正在构建一个与Rails后端对话的应用程序,该后端正在向Rails发送请求。失败了。有没有一种简单的方法可以让Rails日志和spit控制所有传入的HTTP请求?


    如果有人觉得特别仁慈,你可以帮助根本原因。使用简单的curl命令可以很好地工作:

    $ curl -X POST -d "<person><name>Jack</name></person>" -H "Content-Type: application/xml" http://localhost:3000/people.xml
     <?xml version="1.0" encoding="UTF-8"?>
     <person>
      <created-at type="datetime">2009-11-08T16:36:54Z</created-at>
      <id type="integer">3</id>
      <name>Jack</name>
      <updated-at type="datetime">2009-11-08T16:36:54Z</updated-at>
     </person>
    

    但当我的应用程序发送请求时,Rails发出的错误是:

    !故障保险/!\ 2009年11月08日11:38:23-0500 状态:500内部服务器错误 内容正文错误 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/utils.rb:347:in'parse_multipart' /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/utils.rb:319:在“loop”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/utils.rb:319:在“parse-multipart”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/request.rb:141:在“post”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/methodoverride.rb:15:在“call”中 /library/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_controller/params_parser.rb:15:在“call”中 /library/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_controller/session/cookie_store.rb:93:在“call”中 /library/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_controller/failsafe.rb:26:在“call”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:在“call”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:在“同步”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:在“call”中 /library/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_controller/dispatcher.rb:114:在“call”中 /library/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_controller/reloader.rb:34:在“run”中 /library/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_controller/dispatcher.rb:108:在“call”中 /library/ruby/gems/1.8/gems/rails-2.3.4/lib/rails/rack/static.rb:31:在“call”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:46:在“call”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:40:在“each”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:40:在“call”中 /library/ruby/gems/1.8/gems/rails-2.3.4/lib/rails/rack/log_tailer.rb:17:在“call”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/content_length.rb:13:in'call' /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/chunked.rb:15:在“call”中 /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/mongrel.rb:61:in'process' /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in'process_client' /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in'each' /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in'process\u client' /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in'run' /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in“初始化” /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in“new” /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in'run' /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in“初始化” /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in“new” /system/library/frameworks/ruby.framework/versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in'run' /library/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/mongrel.rb:34:在“run”中 /library/ruby/gems/1.8/gems/rails-2.3.4/lib/commands/server.rb:111 /library/ruby/site/1.8/rubygems/custom-require.rb:31:在“gem-original-require”中 /library/ruby/site/1.8/rubygems/custom-require.rb:31:在“require”中 脚本/服务器:3
    3 回复  |  直到 10 年前
        1
  •  2
  •   nathanvda    12 年前

    您最好的选择可能是让前端服务器(Apache、nginx等)把它们吐出来…

    或者,如果您想从Rails上执行此操作,请使用 request.env 在控制器中,例如如下

    logger.info(request.env.inspect)
    
        2
  •  1
  •   Justin Searls    15 年前

    我很感激你的回应,冈德森,尽管这并没有让我找到解决办法。我最终使用了第三方工具, PacketPeeper ,以连接到我的环回设备并报告所有TCP流量。

    我的问题是由于我使用的客户端网络库中的一个疏忽造成的(错误地将我的XML/JSON报告为多部分表单)。

        3
  •  0
  •   AlexChaffee    10 年前

    logger.info request.env 代码在Rails控制器中工作得很好,但是要看到更原始的版本,或者如果您使用的是Grape或其他安装的应用程序,您必须通过机架中间件链拦截请求…

    将此代码放入lib目录(或 application.rb ):

    require 'pp'
    class Loggo
      def initialize(app)
        @app = app
      end
      def call(env)
        pp env
        @app.call(env)
      end
    end
    

    然后和另一个在一起 config S在 应用程序 :

    config.middleware.use "Loggo"