laravel-woocommerce icon indicating copy to clipboard operation
laravel-woocommerce copied to clipboard

JSON ERROR: Syntax error

Open maab16 opened this issue 3 years ago • 24 comments

Sometimes may be you get this error. If you are using it first time then make sure you followed some steps

  1. Enable mod_rewrite in your server
  2. Make sure you have .htaccess file in your WordPress root directory. Contents should look like
<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteBase /
	RewriteRule ^index\.php$ - [L]
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule . /index.php [L]
</IfModule>
  1. Make sure your URL permalink is Post name

maab16 avatar Oct 28 '20 08:10 maab16

I have changed the .htaccess file to as shown however I'm still getting JSON ERROR: Syntax error, is there a work around for this?

alexvolley avatar Nov 12 '20 11:11 alexvolley

I've found this error to show when trying to use the update call on a product, the root cause is the destination shop not accepting PUT requests (http 405 is thrown) as the server is limited to GET and POST requests.

I'll submit a PR that will show the appropriate HTTP error.

SDekkers avatar Dec 15 '20 11:12 SDekkers

Hello @alexvolley and @SDekkers,

Thanks for creating the issue. If you followed above configuration then it's not configuration issue.

For @alexvolley you can check your API key and secret carefully and make sure have read and write permission. For further information follow this link https://woocommerce.github.io/woocommerce-rest-api-docs/#authentication

For @SDekkers I think you need to check your wocommerce account that you have write access.

maab16 avatar Dec 15 '20 15:12 maab16

@maab16 The key used was able to add products, but not update them.. I was able to reverse-engineer the response given by the API, which resulted in the below

(did a dd() on $body in the function processResponse() in the HttpClient)

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n
<html><head>\n
<title>405 Method Not Allowed</title>\n
</head><body>\n
<h1>Method Not Allowed</h1>\n
<p>The requested method PUT is not allowed for this URL.</p>\n
</body></html>\n

This obviously isn't JSON so the syntax error is technically correct, but the error can be better looking at the http status code.

edit: I see the HTTPClient isn't part of this package, so I shall PR it into the other package.

SDekkers avatar Dec 15 '20 15:12 SDekkers

@SDekkers You don't need to use HTTPClient. It already implemented. Can you share your code how yo implement this package?

maab16 avatar Dec 18 '20 13:12 maab16

@maab16 Nothing special..

Product::update($product->woocommerce_id, $data);

The PR I will add to the HTTPClient class in the Automattic\WooCommerce package to improve the error message when the API endpoint throws a HTTP error (i.e. 405 method not allowed)

SDekkers avatar Dec 18 '20 13:12 SDekkers

@SDekkers Sounds looks good. But where you get wocommerce_id in above example?

maab16 avatar Dec 18 '20 13:12 maab16

Just my database, a separate modal with scraped products from a partner webshop.

The issue here is that my clients webshop hasn't allowed PUT and DELETE requests on its webserver, so the error message (JSON Syntax error) was somewhat vague.

SDekkers avatar Dec 18 '20 14:12 SDekkers

@SDekkers Yea, you get the point. Go ahead. It's not this package related issue.

maab16 avatar Dec 18 '20 14:12 maab16

Hey there I still have the similar issue, any advise would be appreciated. regards laravel 7 - php 7.4

CrawDance avatar Jan 03 '21 07:01 CrawDance

I've tested and it worked for me Laravel 7 and PHP 7.4. I think your issue related to the WordPress not Laravel. If you follow the instruction carefully I hope this issue will fix. Thanks

maab16 avatar Jan 03 '21 07:01 maab16

thanks for the response, It was working as intended almost a week ago. no changes no updates, nothing. Maybe a webhosting related issue ?

CrawDance avatar Jan 03 '21 07:01 CrawDance

Faced same issue.

MSufianS avatar Jan 04 '21 12:01 MSufianS

Hi, I'm facing the same issue.

In my case the issue is related to WP installed in a subfolder.

Is there a solution to apply in this case?

Thanks!

[Edit] I solved the issue by moving the site from subfolder to the root directory.

fcampus avatar Jul 12 '21 14:07 fcampus

Same problem here, new Woocommerce installation set up locally, but can't establish a connection.

AIMahn avatar Jul 19 '22 14:07 AIMahn

Hello @AlMahn,

Can you share the error? Thanks

maab16 avatar Jul 19 '22 16:07 maab16

@maab16 When I call the API from the request client it returns JSON and ok. but when I call this form laravel-woocomerce it says the JSON syntax error. maybe it is error in the repo or maybe my configuration fault

saqlainkhadim avatar Oct 31 '22 11:10 saqlainkhadim

@saqlainkhadimbusiness

Please share your request and response. Thanks

maab16 avatar Oct 31 '22 16:10 maab16

Changing the store url from https://example.com to https://www.example.com (so adding www) fixed this issue for me.

StanMenten99 avatar Nov 15 '22 15:11 StanMenten99