imranhsayed / nextjs-woocommerce-restapi

A React WooCommerce Project Example With REST API
nextjs-woocommerce-restapi-virid.vercel.app
MIT License
325 stars 101 forks source link

error: "Request failed with status code 401" #8

Closed Laurynas-Sirgedas closed 3 years ago

Laurynas-Sirgedas commented 3 years ago

When I was trying to reach http://localhost:3000/api/get-products I get an error { success: false, products: [ ], error: "Request failed with status code 401", }

Laurynas-Sirgedas commented 3 years ago

An issue occurred because I've followed all instructions and set the WordPress site address to http://localhost:3000 When I set the Site address (frontend) to a normal domain (the same as WordPress Address) address, then the issue disappears

GPorter43 commented 3 years ago

Is there a more elegant solution to this as using the normal WordPress address will cause the permalinks to link to the wordpress site not the headless Nextjs site

imranhsayed commented 3 years ago

@Laurynas-Sirgedas , @GPorter43

So this is resolved, Please pull the latest changes from https://github.com/imranhsayed/headless-cms or download the latest version of the plugin. Now you should be able to set the SITE URL to the front end url from WordPress General Settings image

image

renjogabro commented 1 year ago

@Laurynas-Sirgedas , @GPorter43

  • Thanks for bringing this up 👍
  • When we set site address url to frontend url, by default wp rest endpoint gets that frontend url as base point.
  • So an actual backend url required an update for REST api only. I have made the changes to the Headlesss CMS Plugin

So this is resolved, Please pull the latest changes from https://github.com/imranhsayed/headless-cms or download the latest version of the plugin. Now you should be able to set the SITE URL to the front end url from WordPress General Settings image

image

@imranhsayed, Many thanks for the wonderful work. However, this is not resolved for me, even though I have the latest version of HCMS. When I set site to localhost:3000 in Wordpress General settings, I get the same result of

{ success: false, products: [ ], error: "Request failed with status code 401", } when I try to reach: http://localhost:3000/api/get-products

It was the same for trying to use function getStaticProps() with this endpoint: /wp-json/rae/v1/header-footer?header_location_id=hcms-menu-header&footer_location_id=hcms-menu-footer. However, this particular problem was solved by adding "index.php" in url, like so:

/index.php/wp-json/rae/v1/header-footer?header_location_id=hcms-menu-header&footer_location_id=hcms-menu-footer

But still not elegant, and would be perfect if this was resolved with the Wordpress General settings.

Any chance you could look into this?