Viewing REST service responses in a browser

Have you ever had this experience? You’re debugging some RESTful service and trying to figure out why it’s returning 500s. So you dump the response body to STDOUT and HOLY HAND GRENADE THAT’S A LOT OF HTML!!! Wouldn’t it be nice if you could just look at it in a browser?

Here ya go:

require 'tempfile'

def browse_string(body)
  Tempfile.open('browse_string') do |f|
    browser = ENV.fetch('BROWSER') { 'firefox' }
    f.write(body)
    f.close
    puts "Starting browser #{browser} with response..."
    unless system(browser, f.path)
      warn "Error starting browser #{browser}"
    end
  end
end

Just drop that in your debug script, in your .irbrc, or make a Boson command out of it. Then call it with the HTML you want to view:

browse_string(response.body)

It even respects the BROWSER environment convention (well, sort of).

I’ve put the code on Gist as well.

Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

Related posts:

  1. Copying Amazon SimpleDB Domains
  2. REST Resources
  3. First and Rest in Ruby
  4. Full Disclosure; or, What’s in your toolbox?
  5. Daemonic Emacs
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.
  • cldwalker

    Cool to see I'm not the only one using Boson :) Lemme know if you start a github repo of Boson libraries.

  • cldwalker

    Cool to see I'm not the only one using Boson :) Lemme know if you start a github repo of Boson libraries.