DEV Community

Cover image for Wait a minute, Mr POSTman
Boyd Duffee
Boyd Duffee

Posted on

Wait a minute, Mr POSTman

Debugging POST request headers in under 40 screen rows. Doesn't actually use Postman

While developing a Perl client for the Harvard Astrophysics Data System API, I was getting errors from my POST requests. Not usually a problem to fix, but I connect to the host machine via a dodgy terminal session that hangs up when the screensaver kicks in or when the wind blows from the East. To keep from having to restart the 6+ windows that I have open in my dev env every time I go for a walk, I start a tmux session running on the host which ignores the SIGHUP and is just where I left it when I reconnect, no .swp files to clean up.

The problem is that I can't scroll up in tmux like I do in a regular terminal1 which leaves me with 40 rows to read the error returned from the POST request. This is nowhere near enough. Hmmm...

💡 Remember that you're using LWP::UserAgent::Mockable (or its Mojo cousin) to record the tests to avoid using the network during the test pipeline. Realise that all the traffic from those network calls are stored as plain text files and you don't have to mess around with tcpdump just to inspect the HTTP headers anymore.

The raw file itself is a bit messy to look at (maybe I should write a quick tool that deserializes it for STDOUT), but it shows me that the Authorization header just isn't there. But it's in my code, I made sure. See right after the call to post ...

Ahh, after some reflection I realize that the post method makes the HTTP request as soon as it's invoked, which is why I was using build_tx in the GET request to add my Dev Key, like so

my $tx = $self->ua->build_tx( GET => $url );
$tx->req->headers->authorization( 'Bearer ' . $self->token );
...
try { $tx = $self->ua->start($tx) }
catch ($error) { ...
}
Enter fullscreen mode Exit fullscreen mode

Now, the response has changed from UNAUTHORIZED to INTERNAL SERVER ERROR. I try the curl command suggested by the docs and that works fine. Look back in the mock file and see ... no payload.

Quietly add the json attribute to the transaction constructor

my $tx = $self->ua->build_tx( POST => $url, json => $hash );
Enter fullscreen mode Exit fullscreen mode

because sending a JSON payload is so damn easy in Mojo.

All done - and Robert is your mother's brother.



  1. why yes, you can scroll up and down in a tmux session when you remember to enter Copy mode with Prefix [ so you get the arrow keys and the Page Up/Down buttons to play with. Enter to exit. 

Top comments (0)