2

我正在运行一个简单的 WEBrick 服务器来调试 POST 数据。我想将 POST 数据输出到日志中。

我的代码是:

server.mount_proc '/' do |req, res|
    res.body = "Web server response:\n"
    # Output POST data here...
end

whereserver只是一个 WEBrick 服务器。

有什么建议么?

4

2 回答 2

5

使用 访问原始帖子数据req.body

server.mount_proc '/' do |req, res|
    res.body = "Web server response:\n"
    p req.body # <---
end

如果您想要解析数据(作为哈希),请req.query改用。

更新

自定义:AccessLog

require 'webrick'

log = [[ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT + ' POST=%{body}n']]

server = WEBrick::HTTPServer.new :Port => 9000, :AccessLog => log
server.mount_proc '/' do |req, res|
    req.attributes['body'] = req.body
    res.body = "Web server response:\n"
end
server.start
于 2013-08-23T08:13:31.700 回答
1

你试过netcat吗?看看你是否有它:

$ man nc

然后你可以像这样启动一个服务器:

$ nc -l 8080    (-l act as a server, listening on port 8080)
(hangs)

如果我将带有数据'a=10&b=20'的发布请求发送到http://locahost:8080,netcat 输出:

$ nc -l 8080    
POST / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: null
Accept-Encoding: gzip, deflate
DNT: 1
Content-Length: 9
Content-Type: text/plain; charset=UTF-8
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

a=10&b=20
于 2013-08-23T08:27:37.083 回答