[英]Unable to get POST working with Yii2 RESTful API
我使用 ActiveController 和基本身份驗證在 Yii2-Basic 中編寫了一個 RESTful API。 我可以讓 GET 方法工作,但是當我使用 Postman Chrome 擴展程序嘗試 Post 時,它會拋出一個錯誤,提示“方法不允許。此 url 只能處理以下請求方法:GET、HEAD。”。
我是否需要在我的 Web 服務器上配置任何東西來測試這個,或者需要控制器中的其他功能? 我什至用一個非常簡單的兩列表格進行了嘗試,並將這些列設置為安全,正如另一個問題中所暗示的那樣。
感謝這方面的任何幫助。 以下是我當前的代碼:
<?php
namespace app\controllers;
use yii\rest\ActiveController;
use yii\filters\auth\HttpBasicAuth;
class TestController extends ActiveController
{
public $modelClass = 'app\models\Test';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::className(),
];
return $behaviors;
}
}
我用來測試的 URL 是: http://localhost/test
嘗試明確允許 POST 方法用於您的操作:
$behaviors['verbs'] = [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
'index' => ['post'],
],
];
原來我使用了錯誤的終點。 使用這個端點對我有用:
試試這個: $params = \\Yii::$app->request->post();
供參考: https : //forum.yiiframework.com/t/rest-api-model-create-problem/83651
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.