woocommerce / woocommerce-rest-api

This is the WooCommerce core REST API Package. It runs standalone as a feature plugin too.
70 stars 46 forks source link

Get product with sku parameter not returning result #228

Closed HammerOz closed 4 years ago

HammerOz commented 4 years ago

https://mysite.com/wp-json/wc/v3/products?sku=product-sku-1

If a product has recently been created by the api, this request will often not return a product for the sku of that product. I also note this returns a 200 response even if not results. Is this response appropriate?

I have turned caching off on the site in question and that seem to resolve the issue.

Example request not returning an result.

Response header: { "connection": "close", "content-length": "22", "content-type": "application/json; charset=UTF-8", "x-robots-tag": "noindex", "link": "<site/>; rel=\"https://api.w.org/\"", "x-content-type-options": "nosniff", "access-control-expose-headers": "X-WP-Total, X-WP-TotalPages", "access-control-allow-headers": "Authorization, Content-Type", "cache-control": "no-transform, no-cache, no-store, must-revalidate", "x-wp-total": "0", "x-wp-totalpages": "0", "allow": "GET, POST", "x-xss-protection": "1", "referrer-policy": "no-referrer-when-downgrade", "x-fw-server": "Flywheel/5.1.0", "x-fw-dynamic": "TRUE", "x-fw-hash": "mend5yxjr7", "content-encoding": "gzip", "x-fw-version": "5.0.0", "server": "Flywheel/5.1.0", "x-cacheable": "NO:Not Cacheable", "fastly-restarts": "1", "accept-ranges": "bytes", "date": "Thu, 30 Jul 2020 07:05:27 GMT", "x-served-by": "cache-vie21645-VIE", "x-cache": "HIT", "x-cache-hits": "2", "x-timer": "S1596092728.565661,VS0,VE1", "vary": "Accept-Encoding", "x-fw-serve": "TRUE", "x-fw-static": "YES", "x-fw-type": "FLYWHEEL_BOT" }

Raw:

{
    "response": {
        "status": 200,
        "headers": {
            "connection": "close",
            "content-length": "22",
            "content-type": "application/json; charset=UTF-8",
            "x-robots-tag": "noindex",
            "link": "<https://site.com.au/wp-json/>; rel=\"https://api.w.org/\"",
            "x-content-type-options": "nosniff",
            "access-control-expose-headers": "X-WP-Total, X-WP-TotalPages",
            "access-control-allow-headers": "Authorization, Content-Type",
            "cache-control": "no-transform, no-cache, no-store, must-revalidate",
            "x-wp-total": "0",
            "x-wp-totalpages": "0",
            "allow": "GET, POST",
            "x-xss-protection": "1",
            "referrer-policy": "no-referrer-when-downgrade",
            "x-fw-server": "Flywheel/5.1.0",
            "x-fw-dynamic": "TRUE",
            "x-fw-hash": "mend5yxjr7",
            "content-encoding": "gzip",
            "x-fw-version": "5.0.0",
            "server": "Flywheel/5.1.0",
            "x-cacheable": "NO:Not Cacheable",
            "fastly-restarts": "1",
            "accept-ranges": "bytes",
            "date": "Thu, 30 Jul 2020 07:05:27 GMT",
            "x-served-by": "cache-vie21645-VIE",
            "x-cache": "HIT",
            "x-cache-hits": "2",
            "x-timer": "S1596092728.565661,VS0,VE1",
            "vary": "Accept-Encoding",
            "x-fw-serve": "TRUE",
            "x-fw-static": "YES",
            "x-fw-type": "FLYWHEEL_BOT"
        },
        "body": []
    },
    "request": {
        "url": "https://site.com/wp-json/wc/v3/products?sku=lamb_shank01",
        "qs": null,
        "headers": {
            "User-Agent": "Integromat/production",
            "Authorization": "***"
        },
        "method": "GET",
        "body": null
    }
}
vedanshujain commented 4 years ago

I have turned caching off on the site in question and that seem to resolve the issue.

Looks like cache plugins on your site are also caching API responses which it should not ideally. I see a x-cacheable response header which is set to NO:Not Cacheable but also a x-cache response header which is set to HIT. Perhaps one or the other caching plugin on your site needs to be configured properly?

Also returning 200 when no results founds is indeed correct, as the request to search for products with a particualr SKU was successful, regardless whether there are any hits or not. For example, we would return 404, when the endpoint itself was not missing (i.e. no functionality to search product with SKU), but since an endpoint did exist and works correctly, a 200 response code was returned.

I am closing this issue as part of repo maintainence, but feel free to comment back if you have any further questions.

HammerOz commented 4 years ago

Ok! Thanks for the feedback. I'll pass it onto to the host who manages the caching.