Skip to content

Instantly share code, notes, and snippets.

@alexvollmer
Created December 4, 2008 17:56
Show Gist options
  • Save alexvollmer/32011 to your computer and use it in GitHub Desktop.
Save alexvollmer/32011 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
##
# A dead-simple way to display formatted JSON output that is the result
# of an HTTP GET request
require "rubygems"
require "json"
require "net/http"
require "uri"
url = URI.parse(ARGV[0])
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
case res['Content-Type']
when /application\/javascript/, /application\/json/, /text\/javascript/, /text\/json/
json = JSON.parse(res.body)
puts JSON.pretty_generate(json)
else
STDERR.puts "Invalid Content-Type for returned response: #{res['Content-Type']}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment