Debugging HTTP requests in a matter of seconds

Published:  18/03/2025 09:37

Introduction

We spend quite a lot of time debugging responses but sometimes we need to know what's in the request.

Here's a way to do it using the php interpreter.

Installing PHP

We're going to need the php CLI utility, which can be installed on any platform.

For instance, there are pre-compiled Windows builds on the Winows PHP website.

It can always be useful to have php around even if you mostly use Docker (and you probably should) to manage your development environment.

Creating the echo script

We can run the script from anywhere but it's better to give it a dedicated directory.

Here's my echo.php:

<?php

$body = file_get_contents('php://input');
$_SERVER['REQUEST_BODY'] = $body;

$s = print_r($_SERVER, true);
error_log($s);

header('Content-Type: application/json; charset=utf-8');

echo json_encode($_SERVER);

We get the request body and put it in the magic variable $_SERVER then we echo everything to both the error output of the server and the response.

In case you don't want the output to be JSON, you can just use print_r($_SERVER); as is and remove the call to header. You'll get the same output as raw text.

Running the script

From the directory where the script currently is, run the PHP dev server (we assume php is reachable from your command line environment):

php -S localhost:8000 echo.php

Feel free to use any other server port.

The dev server will respond with the echo on every possible URLs, and also print the content in the console output for the dev server.

Debugging requests

Here's an example using Insomnia, where we can see the HTTP headers being printed. They're all prefixed with HTTP_.

Shows the debug response in Insomnia

Using curl with an extra header:

Output of an example curl command to the echo server

I pipe the output to the jq utility to format and color the JSON output.

Comments

Loading...