没有不值得去解决的问题,也没有不值得去学习的技术!

Create a GitHub Repository for yiisoft/yii2-app-advanced and Add an API Application Supporting RESTful Web Services and JSON Input

Make a POST request in Postman to http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users with the input format: application/json

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

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

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
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

Make a POST request in Postman to http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users with the input format: multipart/form-data
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

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
Figure 3
JSON

{
    "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

PHP

        '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

JSON

{
    "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

JSON

{
    "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

Make a POST request in Postman to http://api.github-shuijingwan-yii2-app-advanced.localhost/v1/users with the input format: application/json
Figure 4
JSON

{
    "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
    }
}

系列导航

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系