Planets and Webhooks: a simple Flask app

As a Developer Advocate for an API company, I spend a lot of time talking about APIs and webhooks and HTTP in general. Recently I’ve been focussing on HTTP tools, but I really wanted a very simple example API that I could use that would return JSON but really let me focus on the tools, not the API. So I created a “Planets and Webhooks” API with a couple of GET endpoints to return JSON data, and another endpoint to receive and log incoming webhook data. Continue reading

HTTPMock for Testing a Golang API Client

I’m working on an API client for some Nexmo APIs and having tests to work through the various responses that the API can return and check that it does what I expect has been very handy so I thought I’d share my thoughts. I’ve got a bit of a head start here too since the OpenAPI specs have example responses in them that I can grab and feed to the mocking tool I’m using in my tests! Continue reading

HTTP Toolbox

As Web Developers, we need to know how to work with HTTP from every angle. I gave a 2-hour tutorial at PHP UK that included some of my most trusted tools – but it was sold out and a bunch of people asked me if there was video (there wasn’t, tutorials make little sense when videoed). Instead, I thought I’d try to set out a self-study version of the workshop (I rarely teach these days so I’m unlikely to deliver it anywhere else).

There’s a slide deck, some exercises and a sample repo on GitHub … let’s dive in! Continue reading

Test Web Requests with a Local RequestBin

I’ve been a long-time fan of RequestBin, but it’s no longer active since it suffered so much bad traffic. It’s never been too difficult to set up locally and when I tried to do that last week, I realised it has got even easier because it now has a docker-compose configuration. Continue reading

Make a POST Request from PHP With Guzzle

I work extensively with APIs and a variety of serverside scripting languages, and best practice does change over time. Many of the most popular posts on this blog are 10 years old, because apparently I was interesting in 2008. Two in particular from around that time relate to making POST requests from PHP … and I’d do it completely differently today. So, in an attempt to overcome some of the past crimes of the Internet in general and PHP in particular: here’s how to make a POST request in PHP, today, in a PHP 7+ world (it probably works in PHP 5 too). Continue reading

HTTP Tools Roundup

At a conference a few days ago, I put up a slide with a few of my favourite tools on it. I got some brilliant additional recommendations in return from twitter so I thought I’d collect them all in one place in case anyone is interested – all these tools are excellent for anyone working APIs (so that’s everyone!). First, my original slide:

Continue reading

Changing Content Type with Slim Framework

Slim framework has recently invaded my life, I picked it up for a hobby project, recommended it to a client who then contracted me to do quite a lot more development, and it’s also used for m.joind.in. One thing that has tripped me up a couple of times is how to return not-HTML from Slim as it just bins any headers you try to send yourself. I think also that the “right” way to do this may have changed between versions as I also found some examples that didn’t work! What did work for me was this:

        $response = $app->response();
        $response->header("Content-Type", "text/javascript");

The $app variable is the Slim\Slim instance for your application, once you have that, you can just add on any headers you need to with this call to header(). It wasn’t obvious to me and there weren’t a lot of resources for this, so I thought I’d share!

Twitter Search API Using PHP and Guzzle

In case you missed it, Twitter updated their APIs recently, so that you have to authenticate to use even their search APIs to return publicly-available results. This is an increasing trend for API providers, to provide either very limited or nonexistent access for unauthenticated users, I think so they can rate limit consumers that swamp them. To cut a long story short, that meant I needed to update my dashboards that keep an eye on twitter searches to do more than just call file_get_contents in the general direction of the right URL. Continue reading

Setting Multiple Headers in a PHP Stream Context

Last week I tried to create a PHP stream context which set multiple headers; an Authorization header and a Content-Type header. All the examples I could find showed headers built up as a string with newlines added manually, which seemed pretty clunky and not-streams-like to me.

In fact, you’ve been able to pass this as an array since PHP 5.2.10, so to set multiple headers in the stream context, I just used this:

<?php
$options = &#91;"http" => [
    "method" => "POST",
    "header" => ["Authorization: token " . $access_token,
        "Content-Type: application/json"],
    "content" => $data
    ]];
$context = stream_context_create($options);

The $access_token had been set elsewhere (in fact I usually put credentials in a separate file and exclude it from source control in an effort not to spread my access credentials further than I mean to!), and $data is already encoded as JSON. For completeness, you can make the POST request like this:



Pretty-Printing JSON with Python’s JSON Tool

Today’s quick tip is something that was widely retweeted after my “Debugging HTTP” talk at the ever-fabulous WhiskyWeb conference last weekend. When working with JSON on the commandline, here’s a quick tip for showing the JSON in a nicer format:

curl http://api.joind.in | python -mjson.tool

You need python installed, but the JSON extension is probably included, and that’s all you need for this tool. The result is something like:

python-mjsontool

You can also use this approach to present JSON data that has been captured to another file, for example, it’s a handy trick that I use often when developing something with JSON as a data format.