请求体中无效的JSON数据:语法错误后调用Rest API YII2

时间:2022-10-23 19:34:38

When I try to post using Postman,I get this error {"name":"Bad Request","message":"Invalid JSON data in request body: Syntax error.","code":0,"status":400,"type":"yii\\web\\BadRequestHttpException"}

当我尝试使用Postman发布时,我得到了这个错误{"name":"坏请求","message":"请求体中的无效JSON数据:语法错误","code":0,"status":400,"type":"yii\ web\ BadRequestHttpException"}

My controller is

我的控制器

`class CountryController extends ActiveController { public $modelClass = 'app\models\Country';

类CountryController扩展ActiveController {public $modelClass = 'app\models\Country';

public function behaviors()
{
    return [
        [
           'class' => 'yii\filters\ContentNegotiator',
           'only' => ['index', 'view','create'],
           'formats' => ['application/json' => Response::FORMAT_JSON,],

        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'index'=>['get'],
                'view'=>['get'],
                'create'=>['post'],
                'update'=>['put'],
                'delete' => ['delete'],
                'deleteall'=>['post'],
            ],

        ],
    ];
}

}`

} '

added

添加

'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ]

in api/config.php file.

在api /配置。php文件。

Where I am wrong??

我在哪里错了吗? ?

2 个解决方案

#1


0  

Try this

试试这个

public function behaviors()
{
    return [
        [
           'class' => 'yii\filters\ContentNegotiator',
           'only' => ['index', 'view','create'],
           'formats' => ['application/json' => Response::FORMAT_JSON]

        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'index'=>['get'],
                'view'=>['get'],
                'create'=>['post'],
                'update'=>['post'],
                'delete' => ['delete'],
                'deleteall'=>['post'],
            ]
        ]
    ];
}

#2


-1  

I am using wordpress 4.7 on php 7, also getting problems with the empty request body.

我正在使用php 7上的wordpress 4.7,也遇到了空请求体的问题。

My Controller :

我的控制器:

add_action('rest_api_init', function()
{
     register_rest_route('v0', '/accounts/(?P<slug>[a-z0-9_\-]+)/accounts', array(
        'methods' => 'GET',
        'callback' => function($request)
        {
            try {
               return 'hello';
            }
            catch (Exception $e) {
                $error = json_decode($e->getMessage(), true);
                return new WP_Error($error['status_code'], $error['message'], "");
            }
        }
    ));
});

Response Error :

响应错误:

{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}

I did not found any solution rather than changing in WP core : wp-includes/rest-api/class-wp-rest-request.php and chaning line 672 for conditional check for empty body or not.

我没有找到任何解决方案,而不是在WP核心中更改:WP -include /rest-api/类- WP -rest请求。php和chaning第672行用于空体的条件检查。

$params = json_decode( $this->get_body(), true );

#1


0  

Try this

试试这个

public function behaviors()
{
    return [
        [
           'class' => 'yii\filters\ContentNegotiator',
           'only' => ['index', 'view','create'],
           'formats' => ['application/json' => Response::FORMAT_JSON]

        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'index'=>['get'],
                'view'=>['get'],
                'create'=>['post'],
                'update'=>['post'],
                'delete' => ['delete'],
                'deleteall'=>['post'],
            ]
        ]
    ];
}

#2


-1  

I am using wordpress 4.7 on php 7, also getting problems with the empty request body.

我正在使用php 7上的wordpress 4.7,也遇到了空请求体的问题。

My Controller :

我的控制器:

add_action('rest_api_init', function()
{
     register_rest_route('v0', '/accounts/(?P<slug>[a-z0-9_\-]+)/accounts', array(
        'methods' => 'GET',
        'callback' => function($request)
        {
            try {
               return 'hello';
            }
            catch (Exception $e) {
                $error = json_decode($e->getMessage(), true);
                return new WP_Error($error['status_code'], $error['message'], "");
            }
        }
    ));
});

Response Error :

响应错误:

{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}

I did not found any solution rather than changing in WP core : wp-includes/rest-api/class-wp-rest-request.php and chaning line 672 for conditional check for empty body or not.

我没有找到任何解决方案,而不是在WP核心中更改:WP -include /rest-api/类- WP -rest请求。php和chaning第672行用于空体的条件检查。

$params = json_decode( $this->get_body(), true );