Can't access api

I have installed Cockpit in a subfolder on a shared hosting and followed the information provided in https://github.com/agentejo/cockpit/issues/704#issuecomment-405016371 to setup the paths. Now I am able to access the admin interface at https://mydomain.com/cockpit and everything seems to work. But I am not able to accss API. If I am not logged in, every request is redirected to the login and if I am logged in, every request returns status 404. I try to reach the api at https://…n.com/cockpit/api/… What am I doing wrong?

EDIT: Sorry, I had to shorten the last link as the forum allows new users only to post two links in a post. It should also start with mydomain…

EDIT 2:
Some more information about my setup:

  • Cockpit is located at /home/user/public_html/dir/cockpit
  • The document root points to /home/user/public_html
  • A htaccess file is used to redirect everyting to “dir”
  • I use a “defines.php” file like provided in the above link to avoid having “dir” in the URL
  • If I remove the “defines.php”, the “dir” is in the url, but I can access the API.

You need to create an API key in the Cockpit settings for API access and append a ?token=apikey to every api call.

I’m still not sure how to do this with the user account API keys though.

The key is not the problem, as described in the last point of my edit. My collection is public, so no key is needed.

I collected a few defines from tests on different hosts before. Maybe, there is one for you. If you nest your cockpit deeper, you may adjust some paths to something like basename(basename(__DIR__))

Setup (I used PHP and not the API, but I tested the API for the forms endpoint):

- cockpit/... --> Backend
- index.php   --> Frontend
- .htaccess
- ...

strato (shared host):

subdomain points to /dir

Frontend:

define('BASE_URL', '/' . basename(__DIR__));
define('BASE_ROUTE', '/' . basename(__DIR__));
$options['route'] = preg_replace('#'.preg_quote(BASE_URL, '#').'#', '', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), 1);

Backend:

define('COCKPIT_BASE_URL', '/' . basename(__DIR__));
define('COCKPIT_BASE_ROUTE', '/' . basename(__DIR__));
define('COCKPIT_DOCS_ROOT', dirname(__DIR__));

Uberspace6, Uberspace7 (shared host)

works out of the box if using var/www/virtual/$USER/html

If using subomain var/www/virtual/$USER/blog.example.com:

Frontend: .htaccess needs RewriteBase /

Backend: .htaccess needs RewriteBase /cockpit

@jrast I fixed it. Try it with this branch. https://github.com/raffaelj/cockpit/tree/cp-next_fix-api-call-redirect-to-login

If it works for you, I’ll send a pull request.

I moved the COCKPIT_BASE_URL definition above the COCKPIT_API_REQUEST definition and removed one $ sign in bootstrap.php.

See also this discussion: https://github.com/agentejo/cockpit/issues/873

1 Like