Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Rest API Explorer empty on a fresh install on an hosted platform #7623

Closed
bgenere opened this issue Oct 12, 2017 · 28 comments

Comments

Projects
None yet
10 participants
@bgenere
Copy link
Contributor

commented Oct 12, 2017

Bug

This is the first time I am activating the Rest API.
I have followed the documentation but the Explorer is empty. Even the login method does not show up.
There is no information about this issue in the documentation or any pre requisite needed.
I have checked the Dolibarr Demo system to be sure what should be there, and I was able to use it.
I would like to know what's wrong there. Is there anything to check ?

Environment

Expected and actual behavior

Using the URL I should at least see the login class.
Else I get the following json
{"apiVersion":"1","swaggerVersion":"1.2","apis":[],"authorizations":{"apiKey":{"type":"apiKey","passAs":"query","keyname":"api_key"}},"info":{"title":"Restler API Explorer","description":"Live API Documentation","contact":"arul@luracast.com","license":"LGPL-2.1","licenseUrl":"https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"}}

Steps to reproduce the behavior

Use the above URL i.e https://dolibarrv6.encom1.fr/api/index.php/explorer
nothing displays.

Attached files

[Files]
dolibarr.log

@eldy

This comment has been minimized.

Copy link
Member

commented Oct 12, 2017

Can you try this patch 19adbaf

@bgenere

This comment has been minimized.

Copy link
Contributor Author

commented Oct 13, 2017

The issue is already with 6.0.2 which contains this version of the file htdocs/api/class/api.class.php.

I actually install the file and after reproducing the issue, I find out that the file is the same as the original.

I add var_dump($this->r); at the end of the construct method.
This is what I get :
object(Luracast\Restler\Restler)#66 (32) { ["exception"]=> NULL ["cache"]=> object(Luracast\Restler\HumanReadableCache)#68 (0) { } ["url"]=> NULL ["requestMethod"]=> NULL ["requestFormat"]=> NULL ["responseFormat"]=> NULL ["responseCode"]=> NULL ["baseUrl":protected]=> string(28) "https://dolibarrv6.encom1.fr" ["requestFormatDiffered":protected]=> bool(false) ["apiMethodInfo"]=> NULL ["startTime":protected]=> int(1507905438) ["productionMode":protected]=> bool(true) ["refreshCache"]=> bool(false) ["cached":protected]=> NULL ["apiVersion":protected]=> int(1) ["requestedApiVersion":protected]=> int(1) ["apiMinimumVersion":protected]=> int(1) ["apiVersionMap":protected]=> array(0) { } ["formatMap":protected]=> array(0) { } ["writableMimeTypes":protected]=> array(0) { } ["readableMimeTypes":protected]=> array(0) { } ["formatOverridesMap":protected]=> array(1) { ["extensions"]=> array(0) { } } ["filterClasses":protected]=> array(0) { } ["postAuthFilterClasses":protected]=> array(0) { } ["requestData":protected]=> array(0) { } ["authClasses":protected]=> array(0) { } ["errorClasses":protected]=> array(0) { } ["authenticated":protected]=> bool(false) ["authVerified":protected]=> bool(false) ["responseData":protected]=> NULL ["listeners":"Luracast\Restler\EventDispatcher":private]=> array(0) { } ["events":protected]=> array(0) { } }
I expect this help.

Let me know if I should var_dump other informations or add some parameters.

@eldy

This comment has been minimized.

Copy link
Member

commented Oct 22, 2017

The var_dump can't help.
But the dolibarr.log file show

2017-10-12 13:50:32 INFO 176.137.26.90 --- Access to /api/index.php - action=, massaction=
2017-10-12 13:50:32 INFO 176.137.26.90 --- End access to /api/index.php

We should have content between two lines that show the scanning of dolibarr dirs to find api files
Into file htdocs/api/index.php, it seems we don't enter into this part of code:

// Call Explorer file for all APIs definitions
if (! empty($reg[1]) && $reg[1] == 'explorer' && ($reg[2] == '/resources.json' || $reg[2] == '/resources.json/root'))
{
// Scan all API files to load them

$listofapis = array();

$modulesdir = dolGetModulesDirs();
foreach ($modulesdir as $dir)
{
    // Search available module
    dol_syslog("Scan directory ".$dir." for module descriptor to after search for API files");

We should see the log "Scan directory..."

@bgenere

This comment has been minimized.

Copy link
Contributor Author

commented Oct 23, 2017

Ok, I was thinking the issue is with the rule and the content of $reg.

I added few debug lines in /api/index.php. after line 88.

preg_match('/index.php/([^/]+)(.*)$/', $_SERVER["PHP_SELF"], $reg);
// .../index.php/categories?sortfield=t.rowid&sortorder=ASC
echo "TESTING _SERVER <br>";
var_dump($_SERVER);
echo "<br> reg : ";
var_dump($reg);
echo "<br>";
echo "API EXPLORER <br>";

This is the content of $_SERVER :

{ ["PATH"]=> string(28) "/usr/local/bin:/usr/bin:/bin" ["TEMP"]=> string(4) "/tmp" ["TMP"]=> string(4) "/tmp" ["TMPDIR"]=> string(4) "/tmp" ["HTTP_ACCEPT"]=> string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8" ["HTTP_ACCEPT_LANGUAGE"]=> string(35) "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3" ["HTTP_CONNECTION"]=> string(5) "close" ["CONTENT_LENGTH"]=> string(1) "0" ["HTTP_COOKIE"]=> string(69) "DOLSESSID_d4b86a54552143cc219539a26a6dffe8=ou9rcmeqhg2b0psigdd1qe9973" ["HTTP_HOST"]=> string(20) "dolibarrv6.encom1.fr" ["HTTP_USER_AGENT"]=> string(76) "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0" ["HTTP_CACHE_CONTROL"]=> string(9) "max-age=0" ["HTTP_X_REAL_IP"]=> string(13) "176.137.26.90" ["HTTP_X_FORWARDED_HOST"]=> string(20) "dolibarrv6.encom1.fr" ["HTTP_X_FORWARDED_SERVER"]=> string(20) "dolibarrv6.encom1.fr" ["HTTP_UPGRADE_INSECURE_REQUESTS"]=> string(1) "1" ["UNIQUE_ID"]=> string(27) "We41sQTbCU83n5UaJ0ZTwAAAAAw" ["HTTPS"]=> string(2) "on" ["SERVER_SIGNATURE"]=> string(0) "" ["SERVER_SOFTWARE"]=> string(6) "Apache" ["SERVER_NAME"]=> string(20) "dolibarrv6.encom1.fr" ["SERVER_ADDR"]=> string(14) "109.234.165.33" ["SERVER_PORT"]=> string(3) "443" ["REMOTE_ADDR"]=> string(13) "176.137.26.90" ["DOCUMENT_ROOT"]=> string(35) "/home/encommun/dolibarrV6.encom1.fr" ["REQUEST_SCHEME"]=> string(5) "https" ["CONTEXT_PREFIX"]=> string(0) "" ["CONTEXT_DOCUMENT_ROOT"]=> string(35) "/home/encommun/dolibarrV6.encom1.fr" ["SERVER_ADMIN"]=> string(30) "webmaster@dolibarrv6.encom1.fr" ["SCRIPT_FILENAME"]=> string(49) "/home/encommun/dolibarrV6.encom1.fr/api/index.php" ["REMOTE_PORT"]=> string(5) "46082" ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.0" ["REQUEST_METHOD"]=> string(3) "GET" ["QUERY_STRING"]=> string(0) "" ["REQUEST_URI"]=> string(24) "/api/index.php/explorer/" ["SCRIPT_NAME"]=> string(14) "/api/index.php" ["PATH_INFO"]=> string(10) "/explorer/" ["PATH_TRANSLATED"]=> string(45) "/home/encommun/dolibarrV6.encom1.fr/explorer/" ["PHP_SELF"]=> string(14) "/api/index.php" ["REQUEST_TIME_FLOAT"]=> float(1508783537.7707) ["REQUEST_TIME"]=> int(1508783537) }

It seems PHP_SELF does not contains the full URI but only the php source which is compliant with http://php.net/manual/fr/reserved.variables.server.php.
So $reg is an empty array.

I try to use REQUEST_URI instead of PHP_SELF and get $reg as array(3) { [0]=> string(19) "index.php/explorer/" [1]=> string(8) "explorer" [2]=> string(1) "/" }

But by the way, I do not understand how "($reg[2] == '/resources.json' || $reg[2] == '/resources.json/root'))" could be true with this call. So the issue is not solved.

I expect it helps.

@manu-p

This comment has been minimized.

Copy link

commented Nov 24, 2017

Hi, anything new with this issue? I'm afraid I'm facing quite the same behavior with a fresh upgrade of Dolibarr to 6.0.3.

After being able to generate the token using http://website/htdocs/api/index.php/login?login=auserlogin&password=thepassword (without [&reset=1] which leads to an error), I expected to find something at http://website/htdocs/api/index.php/explorer/ but with no success!

I just get this "500 : undefined http://website/htdocs/api/index.php/explorer/resources.json"

And using http://website/htdocs/api/index.php, I get a 404 XML page mentioning:
<source>Routes.php:458 at route stage</source>

I can find a warning in the log file :

2017-11-24 15:04:05 INFO IP@->IP@ Scan directory <...>/htdocs/core/modules/ for module descriptor to after search for API files
2017-11-24 15:04:05 WARNING IP@->IP@ We found an api_xxx file (api_category.class.php) but class Category does not exists after loading file
2017-11-24 15:04:05 INFO IP@->IP@ --- End access to /htdocs/api/index.php/explorer/resources.json

Same question as above, what's wrong? What can I do to make it work?

@manu-p

This comment has been minimized.

Copy link

commented Nov 24, 2017

Solved (as far as I know) on my side, after deleting the following files (some already deleted at one of the latest update step) :

  • compta/facture/class/api_invoice.class.php
    
  • compta/facture/class/api_deprecated_invoice.class.php
    
  • compta/facture/class/api_invoices.class.php
    
  • societe\class\api_contact.class.php
    
  • societe\class\api_thirdparty.class.php
    
  • categories\class\api_category.class.php
    
  • product\class\api_product.class.php
    
@eldy

This comment has been minimized.

Copy link
Member

commented Nov 25, 2017

This is list of old files that may create troubles. Migration tool should remove them.
Remove them manually if not:

DOL_DOCUMENT_ROOT.'/compta/facture/class/api_invoice.class.php',
DOL_DOCUMENT_ROOT.'/commande/class/api_commande.class.php',
DOL_DOCUMENT_ROOT.'/user/class/api_user.class.php',
DOL_DOCUMENT_ROOT.'/product/class/api_product.class.php',
DOL_DOCUMENT_ROOT.'/societe/class/api_contact.class.php',
DOL_DOCUMENT_ROOT.'/societe/class/api_thirdparty.class.php'
@bgenere

This comment has been minimized.

Copy link
Contributor Author

commented Nov 25, 2017

On my installation old files where named "api_deprecated_xxxx".
I have removed them but the issue is still there Explorer stay empty.
If I try to use the login url I get :

404Not Found

And no error in the log file. As previously explorer does not shows anything.

@manu-p

This comment has been minimized.

Copy link

commented Nov 25, 2017

Migration tool should remove them.

Sure! That's what you could expect from such a tool.

And what about the api_deprecated_xxx.php files?

@Finch65

This comment has been minimized.

Copy link

commented Dec 19, 2017

I just did an upgrade from 6.0.2 to 6.0.4 and noticed that the Migration Tool missed checking for:

DOL_DOCUMENT_ROOT.'/categories/class/api_category.class.php'

After deleting that file, as well as the various deprecated class files dispersed over various folders, the API was working again.

@bgenere

This comment has been minimized.

Copy link
Contributor Author

commented Dec 21, 2017

Hello everybody.
I have made extended tests and removed all api old files.
My conclusion is that there is a bug in line 93 of index.php.

As mentioned on October 23rd, the line :
preg_match('/index.php/([^/]+)(.*)$/', $_SERVER["PHP_SELF"], $reg);
is wrong.

When I fix it with
preg_match('/index\.php\/([^\/]+)(.*)$/', $_SERVER["REQUEST_URI"], $reg);
explorer works much better. It actually list all API available and I could expand each one.

However there is still an issue as none of the get method I try to use through the explorer send me an answer.

@tcarterfrance

This comment has been minimized.

Copy link

commented Jan 2, 2018

Hi guys !

Following eldy,25 Nov 2017, I found I needed to delete :
/web/categories/class/api_category.class.php (renaming as "api_category.class.php-" is good)

I still had error :
Unable to read api 'dictionnarytowns' from path http://dolibarr.cergy-internet.net/api/index.php/explorer/resources.json/dictionnarytowns (server returned undefined)
Just a spelling problem (two n's) fixed with :
Delete /web/api/class/api_dictionnarytowns.class.php
Delete /web/api/class/api_dictionnarycountries.class.php

There's also problem with /web/api/class/api_generic.class.php. Index.php doesn't find it as the class is GenericApi instead of Generic. Just correcting this still gave errors so I just deleted it.

Thanks for Dolibarr. I'm a real fan !

Thomas CARTER, Cergy Internet.

@abulte

This comment has been minimized.

Copy link

commented Jan 25, 2018

For those having the same issue with Nginx, you should check out https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/ to ensure that PHP_SELF is correctly set. This can also help: https://serverfault.com/a/626559.

@7PH

This comment has been minimized.

Copy link

commented Mar 1, 2018

Hello @bgenere , I had the same issue.
I replaced both PHP_SELF by REQUEST_URI like you did and fixed api calls, you can see my index.php file here:
https://github.com/7PH/dolibarr/blob/develop/htdocs/api/index.php
Just had to remove the uri parameters from the $reg variable before file inclusion.
Everything works now,
Hope this can help some of you guys!

@ewake

This comment has been minimized.

Copy link

commented Jul 30, 2018

Hi, with this environment:

Dolibarr: 7.0.1
Luracast Restler: 3.0.0-RC6
OS: Linux Ubuntu 16.04.4
Web server: Apache
PHP: 7.2.x
Database: MySQL 5.7.22

i've solved in this way:

  1. updated composer
  2. added "use Luracast\Restler\Restler;" at the beginnig of /api/index.php file
  3. replaced "$api->r->addAPIClass('Luracast\\Restler\\Explorer');" with "$api->r->addAPIClass('Explorer');" on the /api/index.php file
@atm-maxime

This comment has been minimized.

Copy link
Member

commented May 18, 2019

Too old version, please upgrade and reopen if needed.

@atm-maxime atm-maxime closed this May 18, 2019

@bgenere

This comment has been minimized.

Copy link
Contributor Author

commented May 19, 2019

Sorry but now I am working with version 9.0.3 and still problems.

  1. I set the module and then open module settings.
  2. Click the link to the explorer i.e. https://gestion.encom1.fr/api/index.php/explorer/
  3. Get the following error :

{"messages":["attribute paths is not of type object"],"schemaValidationMessages":[{"level":"error","domain":"validation","keyword":"type","message":"instance type (array) does not match any allowed primitive type (allowed: ["object"])","schema":{"loadingURI":"http://swagger.io/v2/schema.json#","pointer":"/definitions/paths"},"instance":{"pointer":"/paths"}}]}

7PH made some changes to the code (see comment above), is there anybody that could check if this make sense ? I do not feel enough confident myself today to make the same changes and submit the result for integration.

@jtraulle

This comment has been minimized.

Copy link
Contributor

commented May 19, 2019

Sorry but now I am working with version 9.0.3 and still problems.

1. I set the module and then open module settings.

2. Click the link to the explorer i.e. https://gestion.encom1.fr/api/index.php/explorer/

3. Get the following error :

{"messages":["attribute paths is not of type object"],"schemaValidationMessages":[{"level":"error","domain":"validation","keyword":"type","message":"instance type (array) does not match any allowed primitive type (allowed: ["object"])","schema":{"loadingURI":"http://swagger.io/v2/schema.json#","pointer":"/definitions/paths"},"instance":{"pointer":"/paths"}}]}

7PH made some changes to the code (see comment above), is there anybody that could check if this make sense ? I do not feel enough confident myself today to make the same changes and submit the result for integration.

This error is not blocking. I have it too but have no problem to use the api ;)

@bgenere

This comment has been minimized.

Copy link
Contributor Author

commented May 19, 2019

This is blocking the explorer.

Even if I enter my API key and then try to get the list of API i get the error message.

If the explorer is not working don't be surprised if nobody uses the API but connect directly to the database through SQL.

The explorer is the only way to get the proper list of API and the proper call syntax. No documentation -> no use.

@jtraulle

This comment has been minimized.

Copy link
Contributor

commented May 19, 2019

Maybe it does not work for you but I can assure you that it works for other people (it is not "globally" broken).
I use the API in prod and it works well.

api

Have you modified anything in the dolibarr files ?

@jtraulle

This comment has been minimized.

Copy link
Contributor

commented May 19, 2019

@bgenere From what I can see, you are running Dolibarr with a nginx webserver.
For the API to work, you need to add this to your nginx serverblock (virtual host) :

fastcgi_read_timeout 180;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
@jtraulle

This comment has been minimized.

Copy link
Contributor

commented May 19, 2019

@bgenere Note that you will still got the Swagger red ERROR badge because the login API endpoint has both GET and POST methods.

If you want to solve that (it won't bother if you do not and API is still usable), you can edit /htdocs/api/class/api_login.class.php and remove the annotation * @url GET /, line 55.

api2

@7PH

This comment has been minimized.

Copy link

commented May 19, 2019

It was more than one year ago, but from what I remember, I was using nginx which was not setting the $_SERVER["PHP_SELF"] superglobal. It was used here: https://github.com/Dolibarr/dolibarr/blob/develop/htdocs/api/index.php#L91 and was breaking the routing system below.

I replaced these usages by $_SERVER["REQUEST_URI"] and it solved my problem, as this diff shows it (might be irrelevant now): develop...7PH:develop

Would not recommend using my old dirty fix, I used this because I did not had access to nginx configuration so this was a temporary workaround for me

@jtraulle

This comment has been minimized.

Copy link
Contributor

commented May 19, 2019

@7PH Thanks for the tip ;) With this config "tip" for nginx, it also works without editing dolibarr files : #7623 (comment)

@7PH

This comment has been minimized.

Copy link

commented May 19, 2019

@jtraulle
Not an nginx expert, but I do not see of this snippet would add PHP_SELF to the server superglobals. Looking at the current code in the master branch, dolibarr is still using it. However it might help with other compatibility issues due to cgi so thanks.

@bgenere
If you are using nginx, I would recommend looking at the content of the $_SERVER superglobal and see if any of the values we mentionned is missing. There is this testing procedure which was very useful for me to check these values are correctly set by nginx. As for PHP_SELF, the solution is given on the bottom of the page:
output

Hope this can be of some help to you, cheers!

@jtraulle

This comment has been minimized.

Copy link
Contributor

commented May 19, 2019

@7PH All I know is that with this config and untouched Dolibarr files, it works on nginx. If i remember well, got this config from a Dolibarr other Github issue ;)

@bgenere

This comment has been minimized.

Copy link
Contributor Author

commented May 20, 2019

Unfortunately I did not have access to the nginx server configuration, I am on an hosted platform and dot not have any control on it.

I have edited the /htdocs/api/class/api_login.class.php and removed the annotation * @url GET /, line 55. and still get the issue.

I also find out that I am forced to use production mode in the module settings (I could not unset the button).

I am attaching the PHP Info information available from the Dolibarr installation, in case somebody could explain which values are not compatible with the API
Infos PHP.pdf

I could change the PHP options which are available in this file. Is there any change that could help.
Options PHP.pdf.

When I look to https://wiki.dolibarr.org/index.php/Module_Web_Services_REST_(developer), I do not see any configuration prerequisite or guidance for using the Rest module. We need to make some progress there, I will be happy to update the documentation and find a solution but clearly this module fail to be as easy as other Dolibarr modules to install and use.

@jtraulle

This comment has been minimized.

Copy link
Contributor

commented May 20, 2019

@bgenere If you are not able to adjust the nginx serverblock config, like explained by @7PH, you can also edit the file htdocs/api/index.php and replace the line 91 from

preg_match('/index\.php\/([^\/]+)(.*)$/', $_SERVER["PHP_SELF"], $reg);

to

preg_match('/index\.php\/([^\/]+)(.*)$/', $_SERVER["REQUEST_URI"], $reg);

This fix still works (I just tested after removing my nginx config to confirm). However be aware that a future Dolibarr update might remove your change so, pay attention to that and check the file htdocs/api/index.php after upgrading Dolibarr 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.