Daniel Watrous on Software Engineering

A Collection of Software Problems and Solutions

Posts tagged debugging

Software Engineering

Echo for REST client debugging

Recently I was debugging a REST client class in Java. The service I was calling simply returned that the request was incomplete. Without access to the service logs and without more meaningful feedback from the service, I decided to create an echo script that would allow me to see exactly what my REST client was sending.

For simplicity I used a LAMP installation on Ubuntu running inside virtual box on my Windows 7 development machine. I could have installed something like WAMPServer, but I prefer to leave my machine as clean as possible. Virtual Box is an ideal choice for this since I can create any type of host with any configuration and leave my own development machine unaffected.

I wrote my echo script in PHP. Here’s the code:

1
2
3
4
5
<?php
print_r($_SERVER);
$raw_post = file_get_contents("php://input");
echo $raw_post;
?>

The script is pretty simple. I just echos back to me what it sees. This includes the headers and the data contained in the BODY of the request. Note that the body will be empty for GET and DELETE (or should be anyway).

Here’s what a call to my service looks like:

As you can see, I can now see exactly what my REST client is sending to any service. This enables me to debug the client until I know that what I’m sending just right.

Note that if it becomes necessary to contact the owner of the REST service, I now have additional details to provide him and help him identify potential problems with his service. This is much better than claiming his service doesn’t work right and later finding that I actually wasn’t sending the request properly.

Resources

Java code to send a POST to a REST client: http://www.exampledepot.com/egs/java.net/post.html