<![CDATA[ RESTful API: simple controller produces 502 Bad Gateway error - My Forums]]> https://forums.modx.com/thread/?thread=104945 <![CDATA[RESTful API: simple controller produces 502 Bad Gateway error]]> https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564286
I followed the steps in Mark Hamstra's 'Developing RESTful APIs' (https://docs.modx.com/revolution/2.x/developing-in-modx/advanced-development/developing-rest-servers).

The first step (Bootstrapping the API) seems to be OK. An undefined endpoint results in the false "success message" as indicated in the document.

But whenever I try to build an API endpoint, I get a 502 Bad Gateway error. Even with the simple "modResource" classKey property.

The nginx error log entry looks like this:

2019/03/02 08:03:04 [error] 17072#17072: *62732 upstream sent invalid status "-1 Internal Server Error" while reading response header from upstream, client: 81.164.224.235, server: domain.com, request: "GET /conversatie/rest/items HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "domain.com"

I ran it on a "fresh" Modx (2.7.1.).

Any clues?

Ludo


]]>
omegerard Mar 02, 2019, 07:17 AM https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564286
<![CDATA[Re: RESTful API: simple controller produces 502 Bad Gateway error]]> https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564488
If you don't have a Menu item in the Manager to call your controller, you may not need the "action" part, but "Controller" may still need to be at the end.

I'd also recommend putting your controller in the core/components/mynamespace/controllers/ directory.

When you load a controller with the path: core/components/mynamespace/controllers/mycontroller.class.php, MODX expects to find a class inside that's named MynamespaceMycontrollerController. If it doesn't, it thinks it has the wrong file and bails out. The first part is the actual name of the namespace. The second is the name of the action, but should match the first part of the filename except for case (filename in lowercase). The third part is just the word "Controller."

The Quotations class isn't really relevant because there's no controller -- it's just a standard HTML form with a snippet.

All that said, your controller file may contain a syntax error that's causing the server error. In that case correcting the controller name won't fix things. A good code editor like PhpStorm is worth its weight in gold for flagging syntax errors that might otherwise take hours to track down, especially since PHP 7 likes to throw a server error instead of a specific PHP error when it finds bad syntax.



]]>
BobRay Mar 18, 2019, 08:34 PM https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564488
<![CDATA[Re: RESTful API: simple controller produces 502 Bad Gateway error]]> https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564485
I'm afraid it led to some confusion as the example in the main documentation

class MyControllerItems extends modRestController {
    public $classKey = 'ToDoItem';
    public $defaultSortField = 'sortorder';
    public $defaultSortDirection = 'ASC';
}


seems to be incompatible with the format you specifiy. The classname 'MyControllerItems' does not contain 'mod' for instance.

By following your instructions on creating a custom (Quotation) DB, I created the file 'quotation.class.php' under 'components/quotes/model/quotes'. I tried to use that classname, but to no avail. Could you explain the classname to use for that quotes table?

Kind regards

Ludo




Quote from: BobRay at Mar 18, 2019, 02:07 AM
MODX is very picky about the class name of a controller, and not very graceful when you get it wrong.

IIRC, the format is: modNamespaceAction...Controller

(The part where the dots are is optional -- it's often "Manager" for CMPs.)

You should also return the controller class name at the end of the class file (outside of the class):

return 'LoginConfirmRegisterController';



]]>
omegerard Mar 18, 2019, 04:11 PM https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564485
<![CDATA[Re: RESTful API: simple controller produces 502 Bad Gateway error]]> https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564484
I've managed to get some feedback from the MODX error log.

It now tells me:

Could not get table name for class: Quotation


Hence I presume that this part of my code (in index.php) has been handled correctly:

$path = $modx->getOption('quotes.core_path', null, 
	   $modx->getOption('core_path').'components/quotes/') . 'model/quotes/';
$modx->getService('quotation', 'quotation', $path);


The php-log doesn't tell me anything. Which might tell you something.

About the name of the controller, see my response to Bob.


Quote from: markh at Mar 17, 2019, 02:45 PM
Can you check your PHP and MODX error logs as well? That hopefully gives a better error message. The nginx one basically just says PHP didn't return what it expected.

Could be an issue where the name of your controller does not match the required/configured naming pattern.


Ludo]]>
omegerard Mar 18, 2019, 03:56 PM https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564484
<![CDATA[Re: RESTful API: simple controller produces 502 Bad Gateway error]]> https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564477
IIRC, the format is: modNamespaceAction...Controller

(The part where the dots are is optional -- it's often "Manager" for CMPs.)

You should also return the controller class name at the end of the class file (outside of the class):

return 'LoginConfirmRegisterController';



]]>
BobRay Mar 18, 2019, 02:07 AM https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564477
<![CDATA[Re: RESTful API: simple controller produces 502 Bad Gateway error]]> https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564475
Could be an issue where the name of your controller does not match the required/configured naming pattern.]]>
markh Mar 17, 2019, 02:45 PM https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564475
<![CDATA[Re: RESTful API: simple controller produces 502 Bad Gateway error]]> https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564458 Quote from: lkfranklin at Mar 04, 2019, 03:48 PM
Can you post what your end point looks like?

Just this one::

class MyControllerItems extends modRestController {
    public $classKey = 'modResource';
    public $defaultSortField = 'id';
    public $defaultSortDirection = 'ASC';
}

under the name Items.php

Ludo]]>
omegerard Mar 15, 2019, 03:14 PM https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564458
<![CDATA[Re: RESTful API: simple controller produces 502 Bad Gateway error]]> https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564290 lkfranklin Mar 04, 2019, 03:48 PM https://forums.modx.com/thread/104945/restful-api-simple-controller-produces-502-bad-gateway-error#dis-post-564290