We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I have been following this to set up a simple API to get datafeed from my resource tree:
    https://docs.modx.com/revolution/2.x/developing-in-modx/advanced-development/developing-rest-servers

    I am hosting this on modx cloud and I keep getting the
    {"success":false,"message":"Method not allowed","object":[],"code":405}
    error.

    My files are in the /rest/ directory at my site root.

    rest/index.php:
    <?php
    // Boot up MODX
    require_once dirname(dirname(__FILE__)) . '/config.core.php';
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    $modx = new modX();
    $modx->initialize('web');
    $modx->getService('error','error.modError', '', '');
    
    // Load the modRestService class and pass it some basic configuration
    $rest = $modx->getService('rest', 'rest.modRestService', '', array(
        'basePath' => dirname(__FILE__) . '/Controllers/',
        'controllerClassSeparator' => '',
        'controllerClassPrefix' => 'MyController',
        'xmlRootNode' => 'response',
    ));
    // Prepare the request
    $rest->prepare();
    // Make sure the user has the proper permissions, send the user a 401 error if not
    if (!$rest->checkPermissions()) {
        $rest->sendUnauthorized(true);
    }
    // Run the request
    $rest->process();


    rest/controllers/programs
    class MyControllerPrograms extends modRestController {
        public $classKey = 'modResource';
        public $defaultSortField = 'id';
        public $defaultSortDirection = 'DESC';
     
        public function beforePost()
        {
            return false;
        }
     
        public function beforePut()
        {
            return false;
        }
     
        public function beforeDelete()
        {
            return false;
        }
     
    }
    }
    
    


    nginx rules
    location /rest/ {
        try_files $uri @modx_rest;
    }
    location @modx_rest {
        rewrite ^/rest/(.*)$ /rest/index.php?_rest=$1&$args last;
    }
    location / {
        try_files $uri $uri/ @modx-rewrite;
    }


    Most ideas on the net are around putting an htaccess in the rest directory, but with nginx this isn't relevant.

    What could be missing in the above set up? I will be calling this api from a gatsby.js (node.js based) site as a data source.

    Thanks