Skip to content

Instantly share code, notes, and snippets.

@dathanb
Last active February 15, 2024 12:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dathanb/8970dd5ca0334f9c1109a6f044fe68b3 to your computer and use it in GitHub Desktop.
Save dathanb/8970dd5ca0334f9c1109a6f044fe68b3 to your computer and use it in GitHub Desktop.
Print Raw HTTP Request and Response Payload in Ruby
module Net
class HTTP
alias_method(:orig_request, :request) unless method_defined?(:orig_request)
def request(req, body = nil, &block)
puts '===== REQUEST ====='
puts "Request: #{req.method} http://#{@address}:#{@port}#{req.path}"
puts 'Request headers: '
req.each_header do |key, value|
puts "\t#{key}: #{value}"
end
if req.method == 'POST'
data = req.body.nil? || req.body.size == 0 ? body : req.body
puts "POST Data: #{data}"
end
response = orig_request(req, body, &block)
puts '===== RESPONSE ====='
puts "Code: #{response.code}"
puts 'Headers'
response.each_header do |header, values|
puts "\t#{header}: #{values.inspect}"
end
puts "Body: #{response.body}"
response
end
end
end
@ankit167
Copy link

ankit167 commented Sep 3, 2018

How do we print the request params for jersey client in ruby?

@kangkyu
Copy link

kangkyu commented Dec 2, 2020

I tried and made the response into a raw response.

puts "HTTP/#{response.http_version} #{response.code} #{response.message}"
response.each_capitalized do |header, values|
  puts "#{header}: #{values}"
end
puts
puts response.body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment