Created
October 28, 2018 18:42
-
-
Save weaming/b4a51df5a2fa57dc8c21eade7f5e1eff to your computer and use it in GitHub Desktop.
This file contains 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
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