7

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?

Luke Francl
  • 31,028
  • 18
  • 69
  • 91

9 Answers9

14

Monkey patch the connection to enable Net::HTTP debug mode. See https://gist.github.com/591601 - I wrote it to solve precisely this problem. Adding this gist to your rails app will give you Net::HTTP.enable_debug! and Net::HTTP.disable_debug! that you can use to print debug info.

Net::HTTP debug mode is insecure and shouldn't be used in production, but is extremely informative for debugging.

Kai Wren
  • 442
  • 3
  • 6
4

Add a new file to config/initializers/ called 'debug_connection.rb' with the following content:

class ActiveResource::Connection
  # Creates new Net::HTTP instance for communication with
  # remote service and resources.
  def http
    http = Net::HTTP.new(@site.host, @site.port)
    http.use_ssl = @site.is_a?(URI::HTTPS)
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
    http.read_timeout = @timeout if @timeout
    # Here's the addition that allows you to see the output
    http.set_debug_output $stderr
    return http
  end
end

This will print the whole network traffic to $stderr.

reto
  • 16,189
  • 7
  • 53
  • 67
3

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.

Matthew Smith
  • 6,165
  • 6
  • 34
  • 35
3

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.)

Ian Terrell
  • 10,667
  • 11
  • 45
  • 66
1

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.

derfred
  • 18,881
  • 3
  • 23
  • 25
0

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!)

Luke Francl
  • 31,028
  • 18
  • 69
  • 91
0

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)

Cameron Booth
  • 6,882
  • 5
  • 28
  • 22
0

I'd use TCPFlow here to watch the traffic going over the wire, rather than patching my app to output it.

mistertim
  • 5,123
  • 4
  • 20
  • 27
-1

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/