Last active
February 15, 2024 12:23
-
-
Save dathanb/8970dd5ca0334f9c1109a6f044fe68b3 to your computer and use it in GitHub Desktop.
Print Raw HTTP Request and Response Payload in Ruby
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried and made the response into a raw response.