Yii2:Rest POST请求参数未到达



早上好,

我对这个话题没有任何进一步的了解,所以我在这里写一个问题。

首先,我用教程中的数据创建了一个DB表:https://www.yiiframework.com/doc/guide/2.0/en/start-databases

然后我用上面的数据从教程中创建了一个Rest控制器:https://www.yiiframework.com/doc/guide/2.0/en/rest-quick-start

教程中的第一个示例GET Request运行良好,它为我提供了数据库中的所有数据。我的请求URL:http://XX.X.X.12:XX90/country/

现在,当我试图通过POST请求在DB中创建一个新的国家时,我们遇到了错误。当使用教程下面的CURL命令和我的测试数据时,我得到以下错误:

SQLSTATE[HY000]: General error: 1364 Field 'code' doesn't have a default value**strong text**
(
[0] => HY000
[1] => 1364
[2] => Field 'code' doesn't have a default value
)

我的restapi的标准日志记录说POST Var是空的,但为什么呢?我还测试了通过工具(Postman(发送POST请求,但我得到了同样的错误。

$_GET = []
$_POST = []
$_FILES = []
$_COOKIE = []
$_SERVER = [....]

我的型号:

<?php
namespace appmodels;
use yiidbActiveRecord;
class Country extends ActiveRecord
{
}

我的控制器:

<?php
namespace appcontrollers;
use yiirestActiveController;
class CountryController extends ActiveController
{
public $modelClass = 'appmodelsCountry';
}

我的CURL请求:

curl -i -H "Accept:application/json" -H "Content-Type:application/json" 
-XPOST "http://XX.X.X.12:XX90/countries/" 
-d '{"code": "TEST", "name": "TestCountry", "population": 01}'

我的web.php配置:

<?php
$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm'   => '@vendor/npm-asset',
],
'name' => 'Yii2-ExtJS Rest API',
'modules' => [
'user' => [
'class' => DaUserModule::class,
// ...other configs from here: [Configuration Options](installation/configuration-options.md), e.g.
'administrators' => ['admin'], // this is required for accessing administrative actions
// 'generatePasswords' => true,
// 'switchIdentitySessionKey' => 'myown_usuario_admin_user_key',
],
'debug' => [
'class' => 'yiidebugModule',
'allowedIPs' => ['XX.X.X.XXX', 'XX.XXX.XXX.XXX', '127.0.0.1', '::1']
],
],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'XXXXXXXXXXXXXXXX',
'parsers' => [
'application/json' => 'yiiwebJsonParser',
] 
],
'cache' => [
'class' => 'yiicachingFileCache',
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => yiisymfonymailerMailer::class,
'viewPath' => '@app/mail',
// send all mails to a file by default.
'useFileTransport' => true,
],
'authManager' => [
'class' => 'yiirbacDbManager',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yiilogFileTarget',
//  'levels' => ['error', 'warning', 'trace', 'info'],
],
],
],
'db' => $db,
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
['class' => 'yiirestUrlRule',
'controller' => 'country'],
], 
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yiidebugModule',
// uncomment the following to add your IP if you are not connecting from localhost.
'allowedIPs' => ['XX.X.X.XX', '::1'],
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yiigiiModule',
// uncomment the following to add your IP if you are not connecting from localhost.
//'allowedIPs' => ['127.0.0.1', '::1'],
];
}
return $config;

建议?

您需要声明使用Rest API 的验证规则

感谢@Bizley

我的国家模式现在:

<?php
namespace appmodels;
use yiidbActiveRecord;
class Country extends ActiveRecord
{
public function rules()
{
return [
[['code', 'name', 'population'], 'required']
];
}
}

最新更新