Take the 2-minute tour ×

I need to capture HTTP response coming after the request made. I have tried with "net/http" gem but it is not giving me full response header. the code I have tried is

uri = URI("http:/example.com")
res = Net::HTTP.get_response(uri)

I am getting some response headers but not full headers, I have checked the same request in firebug and it is giving some extra headers what I am getting by my code

Can any one help me out for this to get full HTTP response headers, or any trick to do that by invoking browser.

share|improve this question
How is this related to watir if you are using Net::HTTP library? –  Željko Filipin Oct 10 '12 at 9:59
I have given my sudo here, but I want to do this in watir, that's why tagged as watir, Is there any way to capture network traffic with watir? –  Gaurav Sharma Oct 10 '12 at 11:55
add comment

3 Answers

Maybe this would help: WebDriver: Y U NO HAVE HTTP Status Codes?!

share|improve this answer
I want to capture full HTTP response header, as in the link you have provided captureNetworkTraffic() does with the selenium, do we have anything similar to this in watir or watir-webdriver so that I can print full request & response header. If you can provide code that will be great help. –  Gaurav Sharma Oct 10 '12 at 13:25
add comment

Try this:

uri = URI("http:/example.com")
res = Net::HTTP.get_response(uri)

If you want to get header information, watir is probably the wrong library for you. What problem are you trying to solve?

share|improve this answer
This I have already tried and getting the header, but my problem is I am not getting full HTTP header as I am getting in firebug when I hit the URL in to browser. When I am hitting the URL in to browser it is giving me lots of request headers, for all the request sent by the browser, but in to those headers there are some more labels or you can say heads are present that I'm not getting using Net::HTTP, You can also try to hit a url in browser and capture the headers in firebug and then hit URL using Net::HTTP, Is there any solution in watir so that i can hit the URL in browser and capture. –  Gaurav Sharma Oct 11 '12 at 6:10
add comment

Not a full answer but more in your direction :)

Impossible using Webdriver (see http://jimevansmusic.blogspot.nl/2012/07/webdriver-y-u-no-have-http-status-codes.html).

Possible solutions:

  1. Use Selenium::Client
  2. Use a proxy

Solution 1 (Selenium::Client):

You can do it using Selenium (also used by Watir Webdriver). Check here: http://blog.testingbot.com/2011/12/21/capture-network-traffic-with-selenium

require "rubygems"
gem "selenium-client"
require "selenium/client"
gem 'test-unit'
require 'test/unit'
# since this code comes from their site (should not be needed)
gem "testingbot"
require "testingbot"

class ExampleTest < TestingBot::TestCase
  attr_reader :browser

  def setup
    @browser = Selenium::Client::Driver.new \
        :host => "hub.testingbot.com",
        :port => 4444,
        :browser => "firefox",
        :version => "8",
        :platform => "WINDOWS",
        :url => "http://www.google.com",
        :timeout_in_second => 60

    browser.start_new_browser_session(:captureNetworkTraffic => true)

  def teardown

  def test_command
    browser.open "/"
    p browser.browser_network_traffic

According to the article this will open Google in Firefox 8 and return the network traffic. An example of a response would be:

"403 GET http://localhost:5555/favicon.ico1333 bytes 94ms 
(2011-12-21T15:53:06.352+0100 - 2011-12-21T15:53:06.446+0100
 Request Headers - Host => localhost:5555 - 
User-Agent => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101
Firefox/8.0.1 - Accept => image/png,image/*;q=0.8,*/*;q=0.5 - 
Accept-Language => en-us,en;q=0.5 - Accept-Encoding => gzip, deflate - Accept-Charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7 - 
Proxy-Connection => keep-aliveResponse Headers - Date => Wed, 21 Dec 2011 14:53:06 GMT - 
Server => Jetty/5.1.x (Windows 7/6.1 x86 java/1.6.0_26 - Content-Type => text/html - 
Content-Length => 1333 - Via => 1.1 (jetty)

Solution 2 (Proxy):

Check http://bmp.lightbody.net/ together with https://github.com/jarib/browsermob-proxy-rb.

share|improve this answer
add comment

Your Answer


