Search code examples
rubyresthttpsrest-client

Getting the last HTTPS Request in Ruby rest-client (like __getLastRequest in php SoapClient)


I'm using the ruby rest-client to send requests to a web service. My requests are working, but I'd like to see the actual request that was sent to the service.

I can't do this with Wireshark or tcpdump because I'm using https and don't have access to the servers private key.

In php, when I've used the SoapClient in the past, I've been able to use the __getLastRequest function to see what xml is sent (http://www.php.net/manual/en/soapclient.getlastrequest.php).

Does anyone know the best way for me to see the actual packets sent to the server?

Many thanks, D.


Solution

  • If you use Net::HTTP instead of rest-client, you can use http.set_debug_output $stderr to see the contents of the request and response:

    require 'net/http'
    require 'openssl'
    uri = URI('https://myserverip/myuri')
    http = Net::HTTP.new(uri.host, uri.port)
    http.set_debug_output $stderr
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Get.new(uri.request_uri)
    request.basic_auth 'username', 'password'
    response = http.request(request)
    @data = response.body
    puts response.body
    

    In addition, using Net::HTTP, I can also get it to use a proxy by using something like http = Net::HTTP::Proxy('127.0.0.1','8888').new(uri.host, uri.port).

    You can also do the same with rest-client by using RestClient.proxy = "http://127.0.0.1:8888/" just before your RestClient.get(url) etc..

    This way I can send the traffic via a tool like Fiddler2 on Windows and then see all the detail I need.

    It's just a shame I can't find an equivalent to Fiddler for the Mac since that's where I want to write the code.

    It Looks like I'll either have to rewrite my code to use HTTP::Net instead of rest-client, or switch to Windows full time ;), unless anyone has any other thoughts.