为什么DELETE方法不允许简单的API控制器

问题描述 投票:4回答:1

基本完全正常工作:

  • GET索引(/ API / V1 /池)
  • GET视图(/ API / V1 /池/ 1)
  • POST创建(/ API / V1 /池)

但是,当我尝试使用DELETE删除(/ API / V1 /池/ 1),我得到以下信息:

不允许的方法

请求的方法DELETE是不允许的URL / API / V1 /池/ 1。

我甚至定义的checkAccess()作为空方法,但似乎没有任何帮助。

设定

控制器:

class PoolController extends \yii\rest\ActiveController
{
    public $modelClass = 'api\models\Pool';

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        // authenticate by using an access token passed in the username authentication header
        $behaviors['authenticator'] = [
            'class' => HttpBasicAuth::className(),
        ];
        return $behaviors;
    }
}

配置:

....
'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    //'enableStrictParsing' => true,
    'rules' => [
    [
        'class' => 'yii\rest\UrlRule', 
        'controller' => ['v1/pool'],
    ],
]
....

请求和响应

卷曲DELETE请求

curl -X DELETE \
  https://###/api/v1/pools/1 \
  -H 'Authorization: Basic dHBDMEUxNWVscl8tNlF6OFVYSGV5V0NydVBwdEp5elM6' \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache'

卷曲响应

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>405 Method Not Allowed</title>
</head><body>
<h1>Method Not Allowed</h1>
<p>The requested method DELETE is not allowed for the URL         
/api/v1/pools/1.</p>
</body></html>

wget的DELETE请求

wget --method DELETE 
  --header 'Content-Type: application/json'   
  --header 'Authorization: Basic dHBDMEUxNWVscl8tNlF6OFVYSGV5V0NydVBwdEp5elM6'   
  --header 'cache-control: no-cache'
  --output-document   - https://###/api/v1/pools/1

wget的DELETE响应

HTTP request sent, awaiting response... 405 Method Not Allowed
2019-02-04 10:25:54 ERROR 405: Method Not Allowed.

为了清楚起见,在同一个控制器中的另一个动作(视图)的工作请求/响应如下所示:

卷曲GET请求

curl -X GET \
  https://###/api/v1/pools/1 \
  -H 'Authorization: Basic dHBDMEUxNWVscl8tNlF6OFVYSGV5V0NydVBwdEp5elM6' \
  -H 'cache-control: no-cache'

卷曲GET响应

{"success":true,"data":{
    "id":1,
    "user_id":1,
    "name":"pool",
    ....
    "created_at":"2019-01-31 20:00:00"
}}

进一步摆弄之后,我发现它总是返回此消息,即使我故意在控制器或API主要配置导致内部错误。工作措施不表明发生内部错误。请求甚至没有在调试器显示,仅GET,POST和HEAD走到这一步。

rest api yii2
1个回答
0
投票

浪费了不少时间后,我发现了这个请求,甚至没有在API调试器(/ API /调试/默认/视图),并在回购的消息的grep显示返回任何内容。这时我才开始期待服务器。

服务器正在运行的Apache / 35年2月4日(UNIX)和/etc/httpd/conf/httpd/extra/httpd-directories.conf有一个规则,阻止所有,但GET,HEAD和POST请求。我加了PUT,PATCH,DELETE和选项。

<Directory /home>
    AllowOverride All
    Options -MultiViews -Indexes +FollowSymLinks +IncludesNoExec +Includes
    AllowMethods GET HEAD POST PUT PATCH DELETE OPTIONS
</Directory>

最后得到它的工作。

© www.soinside.com 2019 - 2024. All rights reserved.