Skip to main content
All CollectionsIntegrationsEcommerceWooCommerce
How to Fix Common Woo-Commerce REST API Issues?
How to Fix Common Woo-Commerce REST API Issues?

Learn how to fix common Woo-Commerce REST API issues with this comprehensive guide.

Sharvari Joshi avatar
Written by Sharvari Joshi
Updated over a week ago

In case you've any time been clung endeavoring to understand Programming interface issues when working with Woo-Commerce, you've come to the ideal spot.

Woo-Commerce has had its own Programming interface since February 2014 with the arrival of Form 2.1 "Perky Penguin", later changing profoundly WordPress REST Programming interface in 2015 with discharge WordPress 4.4 and Woo-Commerce Rendition 2.6.

In any case, regardless of just about 4 years in the wild, it's normal to keep running into "gotcha!" minutes when endeavoring to utilize the REST Programming interface. It's a disappointment that leaves numerous an engineer scratching their head and asking why things aren't working. Indeed, even after a brisk google or search of Stack Trade, rarely do quickly clear what the issue is or how to illuminate.

So in this post, I'll round up a portion of the regular issues we've confronted while endeavoring to interface with a client's store through their Programming interface endpoints and how we've settled them. Ideally it will spare you time wasting time scanning for arrangements.

Woo-Commerce and the WP REST Programming interface: Where It Will, in general, Turn out badly

This won't come as a surprise yet two zones that we've had the most issue with are discovery and authentication.

1. Discovery

In contrast to other REST APIs, the WordPress REST Programming interface is appropriated and accessible independently on each site that supports it. This implies there is no solitary Programming interface root or base to contact.

To help recognize what the root is you can utilize a discovery procedure. The REST Programming interface Handbook gives some great data about a portion of the strategies and procedures you can use to do this.

The handbook prescribes the "link header" technique as the favored method to deal with disclosure. Whenever empowered, the REST Programming interface consequently adds a Connection header to all front-end pages that resembles the accompanying:

By sending a HEAD request to any of your sites' front-end pages, you would then be able to recover the Connection header and parse it for the root URL. Simple.

There are likewise different strategies that can be utilized, for example, checking for a connection meta component in the page source (and parsing it for the root URL like the header technique above), however, they are marginally increasingly confused to execute.

Despite the fact that there are a couple of various strategies accessible in some cases a few (or every one of them) are simply not accessible.

2. Authentication

Not all the data accessible by means of the Programming interface is openly available. To guarantee that just those approved to access said data can do as such, there are various strategies accessible to authenticate clients.

The default strategies utilized by the Woo-Commerce Programming interface are HTTP Essential Verification (which must be performed over HTTPS) and OAuth 1.0a "one-legged" authentication.

OAuth 1.0a is a pain to set up so the most well-known strategy we've found is Basic Authentication. The inconvenience is numerous hosts as well as oversaw administrations don't work out of the crate with a Basic Authentication header and require a few changes in accordance with their arrangement to effectively deal with it.

Issue #1: Caching Plugins Removing Headers

We've discovered that many caching plugins will evacuate headers when serving up cached content. In specific arrangements, both W3 Complete Cache and WP Supercache will evacuate them.

The least demanding technique we've found to recover the header in this situation is to empower the "Cache HTTP headers with page content" choice. Essentially, with W3 All out Cache, in the event that you basically utilize Enhanced Disk Page caching and complete a head demand on a reserved page, you won't perceive any Link header. On the other hand, on the off chance that you rather empower Disk Caching the Connection header returns.

W3TC is very configurable and there are possibilities for "Disable caching of HEAD HTTP requests" and "Specify Additional Page Headers to Cache".

We prescribe counseling with a developer to guarantee there are no undesirable reactions for your particular reserving circumstance when modifying the store treatment of your headers.

It's just somewhat more work, however, we prescribe falling back to check for the connection component meta if the connection header isn't accessible – particularly in the event that you realize you need to manage destinations utilizing storing modules – as the reserving modules don't seem to upset meta components so much.

Issue #2: User Agent Blocking

In the event that you are utilizing a WordPress site to make solicitations to another site by means of the Programming interface and you're utilizing the center HTTP demand strategies, for example, wp_remote_request() or wp_remote_get(), the default Client Specialist utilized is in the accompanying configuration:

WordPress/<<WP VERSION>>; <<HOME_URL>>

Now and then, has, security administrations or potentially security modules might be arranged to square demands that reference the WordPress client operator. This can clearly influence both disclosure and confirmation parts of utilizing the Programming interface in specific situations.

The essential explanation behind blocking dependent on the Client Specialist is to shield against malevolent solicitations from WordPress destinations –, for example, those caused in the (old) WordPress Pingback DDoS Attacks. Administrations, for example, Cloudflare have Web Application Firewall rulesets that may obstruct the solicitations, or your Apache setup may set up to do this either by your host or benevolent neighborhood website specialist. On the off chance that you experience strange conduct when endeavoring to get to a Programming interface, it merits checking for any User Agent blocking.

Issue #3: WordPress Login Page Doesn’t Display Link Headers (or Element)

In the event that you are attempting to perform disclosure on a site that is individuals just, which diverts straight away to wp-login.php (maybe utilizing a module like Force Login) at that point you won't almost certainly utilize the link header or link component revelation process.

Despite the fact that the Programming interface Handbook indicates that "The REST Programming interface naturally adds a Link header to all front-end pages." & "This auto-revelation can be connected to any URL served by a WordPress establishment." the default WordPress login page does not show the Programming interface connect headers or the connection component meta. This sort of bodes is well as it is regularly not a front-end page But rather it can get you out.

Contingent upon how much control you have over the destinations you need to utilize, the revelation procedure on the link header and link component can be added to wp-login.php effectively enough using activities.

A basic module that shows how to include them in is:

<?php // A REST API link element to login page head if ( function_exists( 'rest_output_link_wp_head' ) ) { add_action( 'login_head', 'rest_output_link_wp_head', 10, 0 ); } // Add REST API link header to login page http response if (function_exists( 'rest_output_link_header' )) { add_action( 'login_init', 'rest_output_link_header', 11, 0 ); } ?>

Issue #4: Basic Authentication Headers Are Not Passed Through with PHP in CGI Mode

Another normal issue we've run over is the place the web server facilitating a WooCommerce webpage does not pass the Basic Authentication header data through to PHP in CGI mode of course.

All the more in fact put, what happens is the HTTP Authorization header that your web server (for example Apache) receives in the request doesn't finish up populating the PHP $_SERVER['HTTP_AUTHORIZATION'] variable that PHP uses to populate the $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] factors. These are what WooCommerce utilizes for essential validation.

When the variables are missing, this results in a 401 error with “Consumer secret is invalid” or “Unauthorized” despite the fact that the certifications sent are right. You could take a gander at utilizing OAuth 1.0a however as above it truly can be excruciating to set up. The following most evident path around this is to perform basic authentication by incorporating the accreditations in the URL utilizing question/get parameters, yet that simply isn't verified regardless of whether it is by means of HTTPS and I wouldn't prescribe it.

So how would we unravel the issue utilizing headers? Tragically there doesn't give off an impression of being an across-the-board arrangement that unravels it for everybody. However, here are a few things to attempt on the off chance that you are utilizing Apache.

Solution #1

The arrangement we've discovered that works most of the time is to utilize a basic WordPress .htaccess modification.

# BEGIN WordPress <ifmodule mod_rewrite.c=""> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </ifmodule> # END WordPress

Specifically include E=HTTP_AUTHORIZATION:%{HTTP:Authorization}, in the first [L].

Solution #2

In certain circumstances, the above arrangement will result in apache populating the $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] variable as it were. Apache prepends 'REDIRECT_' to the variable). This by itself isn't utilized by WooCommerce.

The following methodology we've seen function admirably is to rather add the accompanying to your apache arrangement:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

For best outcomes, it can go in your fundamental/worldwide apache arrangement record (for example 'apache2.conf' or 'httpd.conf' or individual site config).

It can likewise be put in your '.htaccess' record over the WordPress rules. Like with Arrangement 1 this will some of the time just outcome in the '$_SERVER['REDIRECT_HTTP_AUTHORIZATION']' variable is populated.

Solution #3

If you are utilizing mod_proxy or mod_proxycgi and Apache 2.4.13+ there is likewise a CGIPassAuthdirective that can be utilized inside your principle apache config record or .htaccess CGIPassAuth on.

Note : These are issues we've encountered direct and worked through to tackle while endeavoring to interface with client stores by means of their Programming interface endpoints. Each of these issues isn't too difficult to comprehend without anyone else when you consolidate a couple on the one site – for example, client operator blocking, storing stripping out link headers and afterward verification headers not going through – it can get incredibly confounded to attempt to make sense of things.


Did this answer your question?