1. The API can distinguish between application/x-www-form-urlencoded and multipart/form-data input formats by default. View the configuration of the request application component in \api\config\main.php
'request' => [
'csrfParam' => '_csrf-api',
],
2. Make a POST request in Postman to http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users with the input format: application/x-www-form-urlencoded, as shown in Figure 1

3. Make a POST request in Postman to http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users with the input format: multipart/form-data, as shown in Figure 2

4. Make a POST request in Postman to http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users with the input format: application/json. Data validation fails, as shown in Figure 3

{
"code": 20004,
"message": "数据验证失败:Username不能为空。"
}
5. Configure the [[yii\web\Request::$parsers|parsers]] property of the request application component to use [[yii\web\JsonParser]] for JSON input. This property is used to convert the raw HTTP request body into $bodyParams
'request' => [
'csrfParam' => '_csrf-api',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
6. Make a POST request in Postman to http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users with the input format: application/x-www-form-urlencoded
{
"code": 10000,
"message": "创建用户成功",
"data": {
"username": "444444",
"email": "444444@163.com",
"password_hash": "$2y$13$qXQwOfXLzPykyk/ymI3z3ua2WofRkrJT5NcmeiyUx8Iko51YdMeg.",
"auth_key": "H8BmGgHLZRGd_1vvU8ESXrAi1pyaL-59",
"status": 1,
"created_at": 1532409559,
"updated_at": 1532409559,
"id": 19
}
}
7. Make a POST request in Postman to http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users with the input format: multipart/form-data
{
"code": 10000,
"message": "创建用户成功",
"data": {
"username": "555555",
"email": "555555@163.com",
"password_hash": "$2y$13$9NByl.XSlo/rzzmmUZWZT.k6Ncpk.br9py1PywpyXRjThsjEQaAMK",
"auth_key": "tQuKPECDLAAqpjDc44CpCK7ynypuLbJ-",
"status": 1,
"created_at": 1532409634,
"updated_at": 1532409634,
"id": 20
}
}
8. Make a POST request in Postman to http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users with the input format: application/json, as shown in Figure 4

{
"code": 10000,
"message": "创建用户成功",
"data": {
"username": "666666",
"email": "666666@163.com",
"password_hash": "$2y$13$SmCEU6tS2qpwU3ggbAkWce5wMZP5BWtqkAgCd.vxbyjN3TwsX32d.",
"auth_key": "D2aVPy___U-aySPYR-1-Y6msxipJulqE",
"status": 1,
"created_at": 1532409697,
"updated_at": 1532409697,
"id": 21
}
}
