Sunday, February 13, 2011

How do I view the HTTP response to an ActiveResource request?

I am trying to debug an ActiveResource call that is not working.

What's the best way to view the HTTP response to the request ActiveResource is making?

  • Maybe the best way is to use a traffic sniffer.

    (Which would totally work...except in my case the traffic I want to see is encrypted. D'oh!)

  • Or my method of getting into things when I don't know the exact internals is literally just to throw in a "debugger" statement, start up the server using "script/server --debugger" and then step through the code until I'm at the place I want, then start some inspecting right there in IRB.....that might help (hey Luke btw)

  • I like Wireshark because you can start it listening on the web browser client end (usually your development machine) and then do a page request. Then you can find the HTTP packets, right click and "Follow Conversation" to see the HTTP with headers going back and forth.

    From MattSmith
  • It's easy. Just look at the response that comes back. :)

    Two options:

    • You have the source file on your computer. Edit it. Put a puts response.inspect at the appropriate place. Remember to remove it.
    • Ruby has open classes. Find the right method and redefine it to do exactly what you want, or use aliases and call chaining to do this. There's probably a method that returns the response -- grab it, print it, and then return it.

    Here's a silly example of the latter option.

    # Somewhere buried in ActiveResource:
    class Network
      def get
        return get_request
      end
    
      def get_request
        "I'm a request!"
      end
    end
    
    # Somewhere in your source files:
    class Network
      def print_request
        request = old_get_request
        puts request
        request
      end
      alias :old_get_request :get_request
      alias :get_request :print_request
    end
    

    Imagine the first class definition is in the ActiveRecord source files. The second class definition is in your application somewhere.

    $ irb -r openclasses.rb 
    >> Network.new.get
    I'm a request!
    => "I'm a request!"
    

    You can see that it prints it and then returns it. Neat, huh?

    (And although my simple example doesn't use it since it isn't using Rails, check out alias_method_chain to combine your alias calls.)

  • This only works if you also control the server:

    Follow the server log and fish out the URL that was called:

    Completed in 0.26889 (3 reqs/sec) | Rendering: 0.00036 (0%) | DB: 0.02424 (9%) | 200 OK [http://localhost/notifications/summary.xml?person_id=25738]
    

    and then open that in Firefox. If the server is truely RESTful (ie. stateless) you will get the same response as ARes did.

    From derfred
  • the firefox plugin live http headers (http://livehttpheaders.mozdev.org/) is great for this. Or you can use a website tool like http://www.httpviewer.net/

  • http://www.jroller.com/bokmann/entry/debugging_activerecord_web_services

0 comments:

Post a Comment