NeDi Community

NeDi Software Specific => Database => Topic started by: nvon001 on February 07, 2020, 08:08:28 PM

Title: API query.php syntax
Post by: nvon001 on February 07, 2020, 08:08:28 PM
Hello, I am hoping to get some ideas/assistance with the API access.  I am using postman and curl to get data on all devices for example, but am unable to query a specific device:
Good Query:
https://1.1.1.1:8443/query.php?t=devices

Bad Query
https://1.1.1.1:8443/query.php?t=devices&q=device="devicea_test"

I receive a ERR :400 Invalid Query.

I can use the sql queries also that gets me the specific device data.  I am not sure how to troubleshoot the API and php.  I am using Mariadb as my database engine.

Thanks for any guidance you can give me on this.
Title: Re: API query.php syntax
Post by: nvon001 on February 08, 2020, 01:14:36 AM
Some more information.  I think I found the place where the error gets triggered located in the query.php file.  I get a "ERR :400 Invalid query!" message.

 }else{
                                        $q = explode(' ',$query);
                                        if( preg_match('/\W/',$q[0]) or !preg_match('/^([=~<>&|]|LIKE)$/',$q[1]) ){
                                        #if( preg_match('/\W/',$q[0]) {
                                                echo "ERR :400 Invalid query!";
                                                die;
                                        }else{
                                                if( $q[1] == '~' ) $q[1] = $dbregexp;
                                                $query = " WHERE $q[0] $q[1] '".DbEscapeString($link,$q[2])."'";
                                        }
                                }
Title: Re: API query.php syntax
Post by: rickli on February 10, 2020, 11:38:36 AM
Try: https://1.1.1.1:8443/query.php?t=devices&q=device+=+"devicea_test"
Title: Re: API query.php syntax
Post by: nvon001 on February 10, 2020, 03:44:00 PM
Perfect thank you, appreciate the quick response!

I got it to work with using what you sent, and removing the quotes:

https://1.1.1.1:8443/query.php?t=devices&q=device+=+devicea_test
Title: Re: API query.php syntax
Post by: nvon001 on February 10, 2020, 07:58:15 PM
An additional question on the API.  Can I refine my API query for example, if I only wanted to display the Serial number of a specific device, or do I need to use a sql client to further my searches?

Example, I want to only show the serial number for a specific device using SQL queries:

SELECT serial FROM devices  WHERE device = 'devicea_test'

Using Postman:
https://1.1.1.1:8443/query.php?t=devices&q=device+=+devicea_test&q=serial+=+*

The postman query above won't work because I obviously don't know the syntax to only display serial number for the devicea_test device.  The SQL client queries seem for me anyways easier.  Do you have information on the syntax involved in different types of queries using the API, or is it one of those things where I need to open up a PHP book, and start reading. 
Only other thing I can do is to pull all the data down for the device and parse it via python for the serial number to display, or just use the SQL client to get at the specific piece of data.
Title: Re: API query.php syntax
Post by: rickli on March 10, 2020, 09:38:59 AM
Sorry for the delay. Really busy with NeDi 2.0! The API is restrictibe due to security, so no selecting columns. I'll work on documentation as soon as 2.0 is finished (in a few weeks)
Title: Re: API query.php syntax
Post by: nvon001 on March 13, 2020, 02:05:52 PM
I appreciate the response.  Looking forward to the documentation, and keep up the good work!
Title: Re: API query.php syntax
Post by: nvon001 on April 09, 2020, 08:18:31 PM
Sorry to keep prodding at this, but I am able to do some workarounds with the API, but am getting stuck now with whitespaces:

https://1.1.1.1:8443/query.php?t=interfaces&q=ifdesc+=+Ethernet1/1 LAG:Po500

I am trying to tie a portchannel to actual physical interfaces by using the 'ifdesc' field, but having that white space between 'Ethernet1/1 LAG:Po500' isn't getting read correctly.  I've tried %20 to replace the whitespace, also tried quotes, but it seems the query is only reading the string before the white space.
Title: Re: API query.php syntax
Post by: rickli on April 15, 2020, 12:08:58 PM
No worries, but due to security restrictions you can't have spaces in the search string (sorry). Can you try ifname instead?