Tuesday, December 2, 2014

An HTTP server for testing


At various times I've needed to check what an HTTP server is receiving from a client system. This can be useful in many scenarios such as if an upstream system is posting data and you don't think it's sending what's expected. The code I present here is a diagnostic tool that lets you see what an HTTP client is sending through.



As this will be a tool I want to use in a variety of environments I've gone for an individual Groovy script. This will let me copy it to a server and get it running quickly. Other than that I'm really just needing a bare-bones HTTP service and the ability to log what it receives.

This approach assumes that Groovy is installed on the server but, if needed, I could package up the compiled Groovy into a jar file and run it under a plain old Java VM.


The following components are utilised in the solution:


The code for this article is located in the Workbench Bitbucket repository.

The codebase consists of a single script (http.groovy) to instantiate an HTTP server and log incoming requests.

To start the HTTP server I just run groovy http.groovy but the following is also supported:

  • groovy http.groovy -h - for usage info
  • groovy http.groovy -p <port> - to change the server port

To send a request to the server I could use a number of tools (even a browser) but the following curl examples are useful:

  • curl --data "param1=value1&param2=value2" 
    • Performs a basic POST request with some query items
  • curl -G --data "param1=value1&param2=value2"
    • Performs a basic GET request with some query items
  • curl -d @test.xml --header "Content-Type:application/xml"
    • POSTs an XML file


I won't break down the script here as I've added enough information within the code. As a diagnostic tool it's important not to see the solution as universal. In its current state the script will provide basic information and, provided you're only POSTing a text-based file, should give you a starting point. I would generally copy this for each specific use and adapt the code (primarily the HttpHandler class) to meet the incoming request.

Further reading

No comments :

Post a Comment