36

I am looking for a command line tool that listens on a given part, happily excepts every HTTP POST request and dumps it.

I want to use it for testing purposes, i.e. for testing clients that issue HTTP POST requests.

That means I am searching the counterpart to curl -F (which I can use to send test HTTP POSTs to a HTTP server).

Perhaps something like socat TCP4-LISTEN:80,fork,bind=127.0.0.1 ... - but socat is not enough because it does not talk HTTP.

3
  • 2
    Maybe I don't understand the question correctly, but if all you need is to dump the POST request, you can use netcat (nc on some systems) with the -l (listen) and -p (port number) options.
    – peterph
    Dec 8, 2012 at 0:23
  • 2
    @peterph, you can use nc for partial testing - but I can see following disadvantages: 1) it does not send HTTP status code 2) I have to hit Ctrl+D after I see the request to close the connection 3) it does not know how to react upon then 'Expect: 100-continue' header 4) it does not know how to handle the 'Transfer-Encoding: chunked' header - it probably just displays the first (probably) empty chunk Dec 8, 2012 at 0:43
  • Similar question on stackoverflow: stackoverflow.com/questions/5725430/… Jul 22, 2015 at 18:33

3 Answers 3

58

I was looking for this myself as well and ran into the Node.js http-echo-server:

npm install http-echo-server -g
PORT=8081 http-echo-server

It accepts all requests and echos the full request including header to the command-line.

0
17

Simple core command line tools like nc, socat seem not to be able to handle the specific HTTP stuff going on (chunks, transfer encodings, etc.). As a result this may produce unexpected behaviour compared to talking to a real web server. So, my first thought is to share the quickest way I know of setting up a tiny web server and making it just do what you want: dump all output.

The shortest I could come up with using Python Tornado:

#!/usr/bin/env python

import tornado.ioloop
import tornado.web
import pprint

class MyDumpHandler(tornado.web.RequestHandler):
    def post(self):
        pprint.pprint(self.request)
        pprint.pprint(self.request.body)

if __name__ == "__main__":
    tornado.web.Application([(r"/.*", MyDumpHandler),]).listen(8080)
    tornado.ioloop.IOLoop.instance().start()

Replace the pprint line to output only the specific fields you need, for example self.request.body or self.request.headers. In the example above it listens on port 8080, on all interfaces.

Alternatives to this are plenty. web.py, Bottle, etc.

(I'm quite Python oriented, sorry)


If you don't like its way of outputting, just run it anyway and try tcpdump like this:

tcpdump -i lo 'tcp[32:4] = 0x484f535420'

to see a real raw dump of all HTTP-POST requests. Alternatively, just run Wireshark.

1
  • 2
    For others who find this very helpful snippet - it does what was asked - but if you want to see the POST body, it's pprint.pprint(self.request.body). Note self.request.body rather than self.body. Same for self.request.headers. See tornado.readthedocs.org/en/latest/…
    – mozz100
    Sep 25, 2014 at 10:17
5

https://hub.docker.com/r/jmalloc/echo-server/

Run

$ docker run -t --rm -p 8080:8080 jmalloc/echo-server
Unable to find image 'jmalloc/echo-server:latest' locally
latest: Pulling from jmalloc/echo-server
fbf67b0844fa: Pull complete
Digest: sha256:617a99b927c3b761621681eb4716582260391c0853b6da904e0f9f1d37785e7a
Status: Downloaded newer image for jmalloc/echo-server:latest
Echo server listening on port 8080

Post

$ curl -XPOST -H"ThisTook: 2 minutes to find" localhost:8080/asdf
Request served by a2d8fa109b92

HTTP/1.1 POST /asdf

Host: localhost:8080
User-Agent: curl/7.54.0
Accept: */*
Thistook: 2 minutes to find
9
  • That docker container just contains a single binary, so you can also docker cp <cid>:/bin/echo-server . to grab the binary. Oct 22, 2020 at 17:09
  • 1
    And do what with it.. and why? Nov 15, 2020 at 7:24
  • run it on your Linux server, since the author doesn't have precompiled binaries. Nov 16, 2020 at 14:32
  • 1
    maybe i am misunderstanding intent here - you want to cp a binary from a docker instance/container and run it directly on the host? As opposed to running it in the container, which is a controlled environment that satisfies any/all needed dependencies of said binary? Nov 17, 2020 at 20:42
  • 1
    That's cool, to bring up months later and for the first time; statically linked, or not, doesn't mean "works on alpine" or "works on anything that it wasn't specifically compiled against (see link)". The reason I am being a dick is because wtf are you pulling a binary out of a container, as opposed to just running the binary in the gd container? Dockerd is universally available, bulletproofed ad nauseam, etc etc. gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63 Jun 1, 2021 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.