3

I'm running a simple WEBrick server to debug POST data. I'd like to output the POST data to the log.

My code is:

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

where server is simply a WEBrick server.

Any suggestions?

Snowcrash
  • 80,579
  • 89
  • 266
  • 376

2 Answers2

5

Access raw post data using req.body.

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

If you want parsed data (as hash), use req.query instead.

UPDATE

Customize :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
falsetru
  • 357,413
  • 63
  • 732
  • 636
1

Have you ever tried netcat? To see if you have it do:

$ man nc

Then you can start a server like this:

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

If I send a post request with the data 'a=10&b=20' to http://locahost:8080, netcat outputs:

$ 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
7stud
  • 46,922
  • 14
  • 101
  • 127
  • On Mac OS X, `nc -l 8080` didn't work for me. I had to use `nc -lp 8080` instead (combining `l` and `p` sounds as if it shouldn't work but seemed to clear up the problem for me). – Snowcrash Aug 23 '13 at 08:45
  • Hmmm...I'm using OSX 10.6.8 and the man pages say: `-l It is an error to use this option in conjunction with the -p option`, and: `-p It is an error to use this option in conjunction with the -l option`. And when I try those two options together, netcat shows me a usage message instead of starting up. What happens when you don't use -p? – 7stud Aug 15 '14 at 00:40