REST API error via AJAX request

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
acentek
Posts: 123
Joined: Thu Jul 27, 2017 2:00 pm

REST API error via AJAX request

Post 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;
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: REST API error via AJAX request

Post by ssax »

Thank you for reporting this! I will get this off to the devs so they can investigate.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: REST API error via AJAX request

Post 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
acentek
Posts: 123
Joined: Thu Jul 27, 2017 2:00 pm

Re: REST API error via AJAX request

Post 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.
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

Re: REST API error via AJAX request

Post by mbellerue »

Excellent, glad you were able to get it working! Closing thread.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.

Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked