1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Refined the verbose output for bin/httparty to look more like curl.

Now when -v/--verbose flag is invoked full HTTP bodies (request and
response) are dumped in HTTP message format (minus some odd
capitalization). This also prevents dumping response codes even when
not asked for (the previous behavior).
This commit is contained in:
Alex Vollmer 2009-03-16 21:30:45 -07:00 committed by Sandro Turriate
parent f19c94c95a
commit 5bc757c1cb

View file

@ -12,11 +12,6 @@ opts = {
:verbose => false
}
def die(msg)
STDERR.puts(msg)
exit 1
end
OptionParser.new do |o|
o.banner = "USAGE: #{$0} [options] [url]"
@ -44,7 +39,7 @@ OptionParser.new do |o|
end
o.on("-H", "--header [NAME=VALUE]", "Additional HTTP headers in NAME=VALUE form") do |h|
die "Invalid header specification, should be Name:Value" unless h =~ /.+:.+/
abort "Invalid header specification, should be Name:Value" unless h =~ /.+:.+/
name, value = h.split(':')
opts[:headers][name.strip] = value.strip
end
@ -54,7 +49,7 @@ OptionParser.new do |o|
end
o.on("-u", "--user [CREDS]", "Use basic authentication. Value should be user:password") do |u|
die "Invalid credentials format. Must be user:password" unless u =~ /.+:.+/
abort "Invalid credentials format. Must be user:password" unless u =~ /.+:.+/
user, password = u.split(':')
opts[:basic_auth] = { :username => user, :password => password }
end
@ -65,21 +60,37 @@ OptionParser.new do |o|
end
end.parse!
puts "Querying #{ARGV.first} with options: #{opts.inspect}" if opts[:verbose]
if ARGV.empty?
STDERR.puts "You need to provide a URL"
STDERR.puts "USAGE: #{$0} [options] [url]"
end
if opts[:output_format].nil?
def dump_headers(response)
resp_type = Net::HTTPResponse::CODE_TO_OBJ[response.code.to_s]
puts "#{response.code} #{resp_type.to_s.sub(/^Net::HTTP/, '')}"
response.headers.each do |n,v|
puts "#{n}: #{v}"
end
puts
end
if opts[:verbose]
puts "#{opts[:action].to_s.upcase} #{ARGV.first}"
opts[:headers].each do |n,v|
puts "#{n}: #{v}"
end
puts
end
response = HTTParty.send(opts[:action], ARGV.first, opts)
puts "Status: #{response.code}"
if opts[:output_format].nil?
dump_headers(response) if opts[:verbose]
pp response
else
print_format = opts[:output_format]
response = HTTParty.send(opts[:action], ARGV.first, opts)
puts "Status: #{response.code}"
dump_headers(response) if opts[:verbose]
case opts[:output_format]
when :json
begin