Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 REST giving a 405 on POST, PUT, etc

I'm trying to build a REST API in a Yii2 advanced application for managing simple queries to my database. Following some tutorials, finally build step by step the example in the Guide, in Quick Start, and get my model working for GET and HEAD methods.

Created my API service inside a module, properly set, with minimal settings, request JSON parser on backend/main.php, registered the module in common/main.php and created the rule in urlManager there too (below the minimal usual rules, enablePrettyUrl, controller/action, ...):

['class' => 'yii\rest\UrlRule', 'controller' => ['precapi']]

But whenever I tried some other methods, via CURL or Postman REST Client, it always gives me a 405 error:

Method Not Allowed. This url can only handle the following request methods: GET, HEAD.

I think I have tried so many different configurations and paths trying to solve it, but without any result. Only thing is 'enableStrictParsing' => false' in urlManager, because it gives me a 404 error on some URLs, and POST does not work with this too, although the application is working as usual.

Any help will be appreciated. Regards.

like image 476
user3627496 Avatar asked May 20 '15 09:05

user3627496


3 Answers

I think is pluralize issue, try this

['class' => 'yii\rest\UrlRule', 'controller' => 'precapi', 'pluralize'=>false],
like image 144
Mijail Paz Avatar answered Dec 09 '22 16:12

Mijail Paz


Without more information on what your precapi controller is based I'm going to assume it's derived from \yii\rest\ActiveController?

If so, you might wat to override the verbs()-function in your controller: The default implementation is this:

protected function verbs()
{
    return [
        'index' => ['GET', 'HEAD'],
        'view' => ['GET', 'HEAD'],
        'create' => ['POST'],
        'update' => ['PUT', 'PATCH'],
        'delete' => ['DELETE'],
    ];
}

I think, given your comments that there is indeed a small problem with the UrlManager configuration, so I went out reading things back and saw you added the controller to a module.
The manual has this to say:

The controller ID (e.g. user, post-comment) that the rules in this composite rule are dealing with. It should be prefixed with the module ID if the controller is within a module (e.g. admin/user).

Might that be the issue?

like image 41
Blizz Avatar answered Dec 09 '22 15:12

Blizz


I am asking on this issue, cause I had it and killed all day long to find the solution. I was very stupid. It is not a problem of YII2 framework, but it's a curl problem. Not the issue with some controllers, actions or behaviors, but with right spelling curl command.

Here is wrong command - delete or put not working and raised 405 error:

curl -i -H "Accept:application/json" -H "Content-Type:application/json" -XDELETE "http://test.test.in/test" -d '{"myid": "2"}'

HTTP/1.1 405 Method Not Allowed Server: nginx/1.8.1 Date: Sun, 26 Jun 2016 07:47:51 GMT Content-Type: application/json; charset=UTF-8 Content-Length: 0 Connection: keep-alive X-Powered-By: PHP/5.6.22 Allow: GET, POST, HEAD, OPTIONS

And here is correct and right way curl's command: curl -i -H "Accept:application/json" -H "Content-Type:application/json" -XDELETE "http://test.test.in/test/2"

HTTP/1.1 204 No Content Server: nginx/1.8.1 Date: Sun, 26 Jun 2016 07:49:19 GMT Content-Type: application/json; charset=UTF-8 Content-Length: 0 Connection: keep-alive X-Powered-By: PHP/5.6.22

That's all, good luck!

like image 33
MasterHans Avatar answered Dec 09 '22 15:12

MasterHans