Skip to content

Instantly share code, notes, and snippets.

@weaming
Created October 28, 2018 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weaming/b4a51df5a2fa57dc8c21eade7f5e1eff to your computer and use it in GitHub Desktop.
Save weaming/b4a51df5a2fa57dc8c21eade7f5e1eff to your computer and use it in GitHub Desktop.
require "http/server"
require "json"
server = HTTP::Server.new do |ctx|
req = ctx.request
body = req.body
body = body ? body.gets_to_end : ""
string = JSON.build do |json|
json.object do
json.field "method", req.method
json.field "host", req.host_with_port
json.field "path", req.path
json.field "query", req.query
json.field "headers" do
json.object do
req.headers.each do |x|
json.field x[0], x[1]
end
end
end
json.field "cookies" do
json.object do
req.cookies.each do |x|
json.field x.name, x.value
end
end
end
json.field "body", body
end
end
ctx.response.content_type = "application/json"
ctx.response.print string
end
addr = server.bind_tcp 8080
puts "Listening on http://#{addr}"
server.listen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment