34

I need to run a simple HTTP server that will only log incoming requests and nothing.

It should log whole requests' content. Like headers, cookies, body....

I need just simple solution that I can run in a few minutes and will work.

Implementation language is not important.

Something like Charles but HTTP server instead of proxy

2 Answers2

52

For some super simple alternatives, there's netcat:

$ nc -l -p 8080

And python's inbuilt:

$ python -m SimpleHTTPServer 8080

(In recent versions of python, 3?) this is now:

$ python -m http.server 8080

Netcat won't serve responses so you may not get too far, SimpleHTTPServer won't show POST requests (at least). But occasionally I find both useful.

rich
  • 18,987
  • 11
  • 75
  • 101
  • 3
    There are multiple versions of netcat out there with different behaviour, irritatingly. The -p flag tends to work even where it's optional. – rich Nov 18 '17 at 18:54
  • 11
    For me on Mac OS X, `nc -l -p ` gave an error, while `nc -l ` worked but only kept nc listening for a single request. `nc -k -l ` keeps it going past the first request. – einnocent Jan 07 '20 at 21:12
  • Be careful with the python http.server command, it will list your directory's files if you request '/' and allow you to download files. – Scen Jun 12 '20 at 20:56
  • `python -m http.server 8080` this one is interesting; But, can I make it to return 200 OK, instead of 404 Not Found? – pilat Mar 23 '22 at 12:25
  • It serves files from the directory you run it in, so if there's an index.html it'll serve that with a 200. If there's nothing in the directory you'll get 404s. – rich Mar 31 '22 at 09:18
26

If you are looking for online HTTP server which will record all requests' information, you can use RequestBin. (Update in 2020: Unfortunately, RequestBin was offline now.)

If you need to make your own HTTP logging server, Node.js + Express is a good candidate. It's very easy and quickly to make:

  1. Install Node.js
  2. Install Express generator so that web application can be generated in seconds: npm install express-generator -g
  3. Generate a web application and install all dependencies:
    express myapp
    cd myapp
    npm install
  1. Edit myapp/app.js and insert following code block before the line app.use('/', index);:
    app.use(function(req, res) {
      console.log(req.headers);
      console.log(req.cookies);
      console.log(req.body);
      res.send('OK');
    });
  1. That's it. Now run DEBUG=myapp:* npm start in console and start the web applicaiton. All HTTP requests sent to host localhost:3000 will be recorded with their headers, cookies and body.
  2. If you need to write these logs into a file, a logger module can be used, such as log.
shaochuancs
  • 15,342
  • 3
  • 54
  • 62