modxcms / revolution

MODX Revolution - Content Management Framework
https://modx.com/
GNU General Public License v2.0
1.36k stars 529 forks source link

modRestService unable to determine the controller when doing a POST request #14831

Closed JoshuaLuckers closed 4 years ago

JoshuaLuckers commented 4 years ago

Bug report

Summary

modRestService is unable to determine the controller when doing a POST request.

When calling modRestService::prepare it calls modRestServiceRequest::setAction with an empty parameter. modRestServiceRequest::setAction will then determine's the action by checking the requestParameter inside $_GET: https://github.com/modxcms/revolution/blob/932472d65793a305bd8c971f21032f24068258ad/core/model/modx/rest/modrestservice.class.php#L282-L295

Therefore when doing a POST request it would fall back to the default action. The action is used to figure out what controller to use when processing the request: https://github.com/modxcms/revolution/blob/932472d65793a305bd8c971f21032f24068258ad/core/model/modx/rest/modrestservice.class.php#L187

Step to reproduce

  1. Set up a simple RESTful API using modRestService class and modRestController.
  2. Create an additional controller with the controllerClassPrefix as prefix (e.g: MyControllerExample where MyController is the defined controllerClassPrefix).
  3. Execute a POST request to the API (e.g: POST yoursite.com/rest/example

Observed behavior

This will produce a method not allowed error because the controller corresponding to the action can not be found.

Expected behavior

When doing a POST request modRestService should find the right controller to process the request.

Workaround

After calling $rest->prepare() (when using the example from the documentation) add the following code: this does not work in all environments

$requestParameter = $rest->request->service->getOption('requestParameter','_rest');
$defaultAction = $rest->request->service->getOption('defaultAction','index');
$action = !empty($_REQUEST[$requestParameter]) ? $_REQUEST[$requestParameter] : $defaultAction;
$rest->request->setAction($action);

Environment

MODX 2.7.2 and up, NGINX

JoshuaLuckers commented 4 years ago

Not a bug, wrong configuration at my part.