Created
December 4, 2008 17:56
-
-
Save alexvollmer/32011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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