gpt4 book ai didi

rest - 为什么在简单的 API Controller 中不允许使用 DELETE 方法

转载 作者:行者123 更新时间:2023-12-01 05:50:50 24 4
gpt4 key购买 nike

基础工作非常好:

  • GET 索引 (/api/v1/pools)
  • 获取 View (/api/v1/pools/1)
  • POST 创建 (/api/v1/pools)

  • 但是当我尝试使用 DELETE delete (/api/v1/pools/1) 时,我收到以下消息:

    Method Not Allowed

    The requested method DELETE is not allowed for the URL /api/v1/pools/1.



    我什至将 checkAccess() 定义为一个空方法,但似乎没有任何帮助。

    设置

    Controller :
    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'],
    ],
    ]
    ....

    请求和响应

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

    curl 响应
    <!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 删除请求
    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.

    为清楚起见,下面显示了同一 Controller 中另一个操作( View )的工作请求/响应:

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

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

    进一步摆弄之后,我发现它总是会返回此消息,即使我故意在 Controller 或 api 主配置中引起内部错误。工作操作确实表明发生了内部错误。该请求甚至没有出现在调试器中,只有 GET、POST 和 HEAD 达到了那个程度。

    最佳答案

    在浪费了很多时间之后,我发现请求甚至没有出现在 api 调试器(/api/debug/default/view)中,并且 repo 中的 grep 消息没有返回任何内容。直到那时我才开始期待服务器。

    服务器正在运行 Apache/2.4.35 (Unix) 并且在/etc/httpd/conf/httpd/extra/httpd-directories.conf 中,有一条规则会阻止除 GET、HEAD 和 POST 请求之外的所有请求。我添加了 PUT、PATCH、DELETE 和 OPTIONS。

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

    终于让它工作了。

    关于rest - 为什么在简单的 API Controller 中不允许使用 DELETE 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54509318/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com