Yii2 REST gives 405 on POST, PUT, etc.

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

Created my API service inside the module, correctly configured with minimal settings, requested the JSON parser on backend / main.php, registered the general module /main.php and created a rule in urlManager there too (below the minimum normal rule, enablePrettyUrl, controller / action ,. ..):

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

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

The method is not allowed. This URL can only process the following request methods: GET, HEAD.

I think I tried so many different configurations and paths, trying to solve it, but without any results. In urlManager there is only 'enableStrictParsing' => false' because it gives me a 404 error on some URLs, and POST does not work with this either, although the application works as usual.

Any help would be appreciated. Sincerely.

+7
yii2 yii2-advanced-app
source share
4 answers

Without the additional information that your precapi controller is based, am I going to assume that it \yii\rest\ActiveController from \yii\rest\ActiveController ?

If so, you can refuse to override the verbs() function in your controller: The default implementation is as follows:

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

I think, taking into account your comments, that there really is a small problem in the UrlManager configuration, so I read the information again and saw that you added the controller to the module.
The manual has this to say :

The identifier of the controller (for example, user, post-comment) with which the rules in this composite rule are associated. It must have a module identifier prefix if the controller is inside the module (for example, admin / user).

Perhaps this is a problem?

+5
source share

I think this is a plurality problem, try this

 ['class' => 'yii\rest\UrlRule', 'controller' => 'precapi', 'pluralize'=>false], 
+7
source share

I ask for this because I killed him all day to find a solution. I was very stupid. This is not a YII2 structure problem, but a twist problem. Not a problem with some controllers, actions, or behavior, but with the right spelling command.

Here is the incorrect command - delete or place the broken one and causing error 405:

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, Jun 26, 2016 7:47:51 GMT Content-Type: application / json; encoding = UTF-8 Content-length: 0 Connection: save life X-Powered-By: PHP / 5.6.22 Allow: GET, POST, HEAD, OPTIONS

And here is the correct and correct curl 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, Jun 26, 2016 07:49:19 AM GMT Content-Type: application / json; encoding = UTF-8 Content-length: 0 Connection: save life X-Powered-By: PHP / 5.6.22

Well, good luck!

0
source share

I wanted to add a possible problem and solution that I encountered today. It has an extra / in the url something like http://localhost:8080/index.php/module/controller_id/?access-token=_token_here

Note how thin / in the url is after the controller_id

This was my problem and I decided to remove the extra /

0
source share

All Articles