How to view the HTTP response to an ActiveResource request
Asked Answered
O

8

6

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

How can I view the HTTP response to the request ActiveResource is making?

Odeliaodelinda answered 22/10, 2008 at 23:31 Comment(0)
A
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.)

Aga answered 25/10, 2008 at 13:9 Comment(0)
P
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.

Paoting answered 24/8, 2011 at 12:30 Comment(0)
B
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.

Baluster answered 7/11, 2011 at 18:25 Comment(0)
J
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.

Janes answered 23/10, 2008 at 2:58 Comment(0)
A
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.)

Aga answered 25/10, 2008 at 13:9 Comment(0)
I
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.

Isochor answered 26/10, 2008 at 9:20 Comment(0)
F
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)

Fablan answered 23/10, 2008 at 2:6 Comment(0)
R
0

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

Reinsure answered 26/9, 2014 at 9:17 Comment(0)
O
-1

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

Odeliaodelinda answered 22/10, 2008 at 23:31 Comment(1)
Charles’ HTTPS proxy is super useful for this sort of thing.Pikeperch

© 2022 - 2024 — McMap. All rights reserved.