Page 1 of 1

REST API error via AJAX request

Posted: Wed Oct 16, 2019 12:07 pm
by acentek
I have discovered a potential bug in the REST API.

When performing an AJAX request to any of the API GET endpoints from a separate website, a 403 Invalid Method response is returned.
It looks like the browser is making an OPTIONS request to the endpoint before making its GET request.
Looking through the source code for the API, it handles POST, GET, PUT, and DELETE requests, but not OPTIONS requests.
Since there is no way to handle the OPTIONS request, it defaults to a 403 error and prevents the GET request from executing.

I was able to resolve this issue by manually adding the following case to the switch statement in the __construct function for the utils.inc.php file for the API:

Code: Select all

case 'OPTIONS':
    $this->_response('Ok', 200);
    break;

Re: REST API error via AJAX request

Posted: Wed Oct 16, 2019 1:33 pm
by ssax
Thank you for reporting this! I will get this off to the devs so they can investigate.

Re: REST API error via AJAX request

Posted: Wed Oct 16, 2019 1:45 pm
by ssax
The devs believe this is directly related to CORS stuff and you should be able to working around in the request from your application, see here:

https://stackoverflow.com/questions/299 ... disable-it

Re: REST API error via AJAX request

Posted: Thu Oct 17, 2019 12:14 pm
by acentek
I figured out the issue.

I was using the Axios library to make the API request. The requests that were created by Axios were not simple enough, so the browser required the OPTIONS request to be sent before the GET request. An API Key is required for API access, but is not sent with the OPTIONS request.

I switched to using JQuery's AJAX functionality and that resolved my issue.

Thank you for the assistance.

Re: REST API error via AJAX request

Posted: Thu Oct 17, 2019 12:19 pm
by mbellerue
Excellent, glad you were able to get it working! Closing thread.