Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

WordPress Site Health "The REST API encountered an error": read the code, then find what blocks the request

Technical article
Hosting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

In WordPress 6.7.1, Site Health shows "The REST API encountered an error" as a critical issue when the server's own request to the REST API gets no HTTP answer at all. The "REST API Response" line under it holds an error code and message, for example a cURL error. Two milder results use the same test: "The REST API encountered an unexpected result" means the server answered with a status other than 200, and "The REST API did not behave correctly" means it answered 200 but ignored the context query parameter. The response line tells you which one you have.

What the error means

Site Health's own description reads: "The REST API is one way that WordPress and other applications communicate with the server. For example, the block editor screen relies on the REST API to display and save your posts and pages."

WordPress 6.7.1 makes a GET request to the wp/v2/types/post route with context=edit added. It sends your session cookies and a REST nonce in the X-WP-Nonce header, waits at most 10 seconds, adds Basic authentication when the visitor arrived with it, and by default does not verify the certificate. The address comes from rest_url(), which is built from the Site Address (home URL). Without pretty permalinks the address takes the form index.php?rest_route=.

Our own reading: the request is made by the web server to itself, so a site can work for visitors and still fail this test.

Label on the screen Status What triggered it
"The REST API encountered an error" Critical WordPress got an error instead of an HTTP answer. The screen prints REST API Endpoint: and REST API Response: (code) message
"The REST API encountered an unexpected result" Recommended The HTTP status was not 200. The response line shows the number in brackets, then the status message
"The REST API did not behave correctly" Recommended The answer was 200 but the decoded body had no capabilities entry. The text says "The REST API did not process the" context "query parameter correctly."

The cURL part of a critical message uses libcurl's numbers: 6 is "Could not resolve host", 7 is "Failed to connect() to host or proxy" and 28 is "Operation timeout".

Likely causes

Rows 2 to 5 are our own reading of what leads to each label. The labels and status numbers are what the code prints.

Cause What you see How to confirm
The server cannot resolve or reach its own address, or the answer takes over 10 seconds Critical, cURL error 6, 7 or 28 curl to the site from the server fails the same way
/wp-json/ returns 404 Unexpected result, (404) Step 3 below: ?rest_route=/ works, /wp-json/ does not
A security plugin, firewall or rule answers 401, 403 or a 5xx Unexpected result with that number Step 2 below shows the same status
The web server drops the query string "did not behave correctly" The _fields test in step 4 returns every field
A 200 page that is not JSON, such as a cache or challenge page "did not behave correctly" Step 2 shows HTML where JSON should be
A PHP session was left open Site Health also lists "An active PHP session was detected" The same Status tab

Prove the cause before changing anything

All of these steps only read. Run the curl lines on the web server itself.

  1. Go to Tools > Site Health > Status and expand the REST API entry. Write down the label, the endpoint address and the response line.
  2. Request the REST root, which needs no sign-in. The -i option shows the response headers.
curl -sS -i https://www.example.com/wp-json/

A working site returns 200 and a JSON body. A 404, 401, 403 or 5xx matches an "unexpected result". Our own reading: an HTML body with status 200 matches "did not behave correctly", because json_decode() returns null when the text cannot be decoded, and null is not false.

  1. Compare the two address forms. The REST handbook says the Link header looks like <http://example.com/wp-json/> on a site with pretty permalinks and <http://example.com/?rest_route=/> on one without. Request the second form.
curl -sS -i 'https://www.example.com/?rest_route=/'

Our own reading: if this returns JSON and step 2 returns 404, WordPress works and the web server is probably not passing /wp-json/ to it. The permalink setting under Settings > Permalinks and the rewrite rules are the other places to look.

  1. Test whether query strings arrive. The REST handbook's _fields parameter limits a response to the named fields.
curl -sS 'https://www.example.com/wp-json/wp/v2/types/post?_fields=slug'

A short object holding only slug means the query string arrives. Our own reading: a long object with name, description and other fields suggests the query string was dropped, although a cache can give the same result.

  1. Look for "An active PHP session was detected" on the Status tab. WordPress.org says a session created by session_start() "interferes with REST API and loopback requests".
  2. On Settings > General compare Site Address (URL) with the address you used above.

Fix

Choose the route that matches what you proved.

The server cannot reach its own address

The same server-side cause breaks the loopback test, so follow WordPress Site Health "Your site could not complete a loopback request": read the result, then find what blocks the request. Do not switch the REST API off: the REST handbook says "You should not disable the REST API; doing so will break WordPress Admin functionality that depends on the API being active."

A plugin or rule returns 401, 403 or a 5xx

The REST handbook shows that a site can require sign-in for every REST request by hooking rest_authentication_errors, and that its example returns a 401 for anyone not logged in. Our own reading: such a rule should not reject the Site Health test while the administrator's cookies and nonce reach WordPress, so a 401 or 403 more likely comes from a firewall, authentication layer or cache that strips the cookies or the header. Our own advice: ask whoever runs the firewall or security plugin to let the server's own requests through rather than removing the rule for everyone.

To test whether a plugin is the cause, first save the current state: write down the names and status of the active plugins.

wp plugin list --status=active

Also write down the current time.

Risk: the next command deactivates one plugin on the live site. Every feature that plugin provides stops at once, including forms, caching, security rules or a shop if that is what it runs.

wp plugin deactivate example-plugin

Reload Tools > Site Health > Status. If the REST entry moves to the passed tests, that plugin was the cause.

To undo it, only if you need the plugin back: activate the plugin you wrote down in the list of active plugins. The change you made, the deactivation, is lost.

Risk: the next command activates the plugin again, so the REST failure returns if that plugin was the cause.

wp plugin activate example-plugin

Then reload Tools > Site Health > Status and check the REST entry.

/wp-json/ returns 404, or the query string is dropped

Our own reading: when ?rest_route=/ works and /wp-json/ does not, or when query strings vanish, the cause is in the web server's configuration, so it is for the server's administrator. For a dropped query string on nginx, the REST handbook's remedy is to find the try_files line in the site configuration, and where it reads try_files $uri $uri/ /index.php$args; change it to try_files $uri $uri/ /index.php$is_args$args;, because $is_args "will print a ? character if query arguments are found". Hand the administrator that line, the page address and your step 4 output. Our own advice: ask them to save the configuration file first and to check it before reloading the web server.

A PHP session is open

WordPress.org's remedy is that the session "should be closed by session_write_close() before making any HTTP requests". That is a change in plugin or theme code, so pass the finding to its developer.

Example (illustrative): on web01, an nginx server for www.example.com, Site Health reports "The REST API did not behave correctly". Step 2 returns 200 and JSON. Step 4 returns a long object with every field instead of only slug. The administrator finds try_files $uri $uri/ /index.php$args; in the server block and asks the person who manages web01 to apply the handbook's $is_args form. No WordPress setting is touched.

Confirm the fix

  1. Reload Tools > Site Health > Status. The test runs again and the entry should move to the passed tests with the label "The REST API is available".
  2. Repeat step 2 and step 4 from the web server. Expect JSON, and a short object holding only slug.
  3. Open Posts > Add New. The block editor should load, and a draft should save without an error banner.

What to watch afterwards

  • Run Site Health again after a change of permalinks, a web server or CDN rule change, a security plugin update, or a hosting move.
  • The REST test and the loopback test fail for related reasons. If both appear, solve the loopback result first.
  • Our own advice: if the block editor fails to save while Site Health passes, note the status of the failing REST request in the browser's developer tools and test that address with curl as in step 2.

Risks and limits

  • These commands have not been run or lab-tested.
  • The wording above is from WordPress 6.7.1. Other versions may differ.
  • The test runs as the signed-in administrator, with cookies and a nonce. A curl request from the command line carries neither, so it can show the web server's side of the problem but not reproduce the test exactly.
  • The web server configuration differs from site to site, so the nginx line and the rewrite checks are for the server's administrator to apply.

Sources