API Debugging with the Profiler

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Debugging an API... can be tough... because you don't see the results - or errors - as big HTML pages. So, to help us along the way, let's level-up our debugging ability! In a traditional Symfony app, one of the best features is the web debug toolbar... which we don't see down here right now because it's not installed yet.

Using the Profiler in an API

But... should we even bother? I mean, it's not like we can see the web debug toolbar on a JSON API response, right? Of course we can! Well, sort of.

Find your terminal and get the profiler installed with:

composer require profiler --dev

You can also run composer require debug --dev to install a few extra tools. This installs the WebProfilerBundle, which adds a couple of configuration files to help it do its magic.

Thanks to these, when we refresh... there it is! The web debug toolbar floating on the bottom. This is literally the web debug toolbar for this documentation page... which probably isn't that interesting.

But if we start making requests... check it out. When we execute an operation via Swagger, it makes an AJAX request to complete the operation. And Symfony's web debug toolbar has a cool little feature where it tracks those AJAX requests and adds them to a list! Every time I hit execute, I get a new one!

The real magic is that you can click the little "sha" link to see the profiler for that API request! So... yea! You can't see the web debug toolbar for a response that returns JSON, but you can still see the profiler, which contains way more data anyways, like the POST parameters, the request headers, request content - which is really important when you're sending JSON - and all the goodies that you expect - cache, performance, security, Doctrine, etc.

Finding the Profile for an API Request

In addition to the little web debug toolbar AJAX tracker we just saw, there are a few other ways to find the profiler for a specific API request. First, every response has an x-debug-token-link header with a URL to its profiler page, which you can read to figure out where to go. Or, you can just go to /_profiler to see a list of the most recent request. Here's the one for /api/cheese_listings. Click the token to jump into its profiler.

The API Platform Panel

Oh, and API Platform adds its own profiler panel, which is a nice way to see which resource this request was operating on and the metadata for it, including this item operation and collection operation stuff - we'll talk about those really soon. It also shows info about "data providers" and "data persisters" - two important concepts we'll talk about later.

But before we get there, back on the documentation page, we need to talk about these five endpoints - called operations - and how we can customize them.

Leave a comment!

  • 2020-05-06 Diego Aguiar

    Hey JP Fortuno

    Are you on Windows WSL? Because it has a very poor performance with files operations.
    Another thing to consider is the first time you execute a request Symfony has to bootstrap the entire app but it creates a cache, so, if you don't modify any file after that, the subsequent requests ought to be faster

    Cheers!

  • 2020-05-06 JP Fortuno

    Hi, any idea as to why a request to fetch an id takes around 2.4 seconds? Quite slow. Is it because I'm running Windows 10 or did I miss something? My other pure web SF4 projects run a request in less than 500ms on the same platform