WordPress

Easy WordPress REST API debugging

By:

Mark Biek

on 8/7/2017

Debugging is hard

Usually, debugging an endpoint goes a little like this:

1. Add some debug code to your REST endpoint
2. Go to your website
3. Fill out a form (or whatever)
4. Submit the form
5. Open the Chrome Inspector
6. Look at the response from the endpoint
7. Repeat

That’s too many steps!

If we only working on the back-end endpoint, we can simplify our debugging by sending the same request over and over using cURL on the command-line.

Grab your request data

The first step is to get a data payload and save it to a file so you can send it to your endpoint. (We’re assuming you’re sending and receiving JSON.)

You can craft one by hand if your endpoint is simple enough, but that’s usually not the case. Most of the time, it’s easier to grab the data from a browser request.

Chrome makes this super easy.

1. Open the Developer Tools and click on the Network tab

2. Click on your request in the list of network requests.

3. Click on the Headers tab and scroll down to “Request Payload” and click “View Source”

4. That will show you the raw JSON of your request which you can copy & paste into a file.

Make ALL THE REQUESTS

Once you have you JSON in a file (let’s say it’s called file.json), making the request is easy.

curl -X POST -d @file.json -H 'Content-Type: application/json' "http://myhost.com/wp-json/restendpoint"

Now you can easily make the same request over and over while you work on the endpoint. Plus you can put all kinds of debugging printouts in the endpoint and you can see the output.

Share to

Related Posts

Custom Dev
Dynamic Select Fields with Advanced Custom Fields
Dynamic Select Fields with Advanced Custom Fields

By:Nick Stewart on 12/8/2021

In Advanced Custom Fields, there is the Post Object field that allows you to select a post resource from your WordPress website. This field can return the actual post object or the post ID.

Read More »
WordPress REST API: Secure ajax calls custom endpoints

By: Mark Biek on 5/9/2017

Here’s a handy guide to making secure ajax calls to custom REST endpoints. As you may have heard, the WordPress REST API is now included out-of-the-box as of WP 4.7.

Read More »