Skip to content

Instantly share code, notes, and snippets.

@ktimothy
Created December 6, 2021 09:18
Show Gist options
  • Save ktimothy/ac7c7b14d10f4c879d8acf1d8c85965f to your computer and use it in GitHub Desktop.
Save ktimothy/ac7c7b14d10f4c879d8acf1d8c85965f to your computer and use it in GitHub Desktop.
class LogRequestAndResponse
def initialize(app); @app = app; end
def call(env)
req = Rack::Request.new(env)
headers = Hash[*env.select {|k,v| k.start_with? 'HTTP_'}
.collect {|k,v| [k.sub(/^HTTP_/, ''), v]}
.collect {|k,v| [k.split('_').collect(&:capitalize).join('-'), v]}
.sort
.flatten]
puts "<-- #{req.request_method} #{req.url} #{req.params.inspect} #{headers.inspect}"
status, headers, body = @app.call(env)
puts "--> #{status} #{(JSON.load(body.first) rescue '- stripped -').inspect} #{headers.inspect}"
[status, headers, body]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment