通过cURL调试Portal等服务

登录获取Token

Portal:https://192.168.104.100:8090/portal/auth/login/

调用登录接口,获取Token信息。

  • 如果是https接口一定要加 -k 参数, 跳过证书检查。
  • 参数是application/x-www-form-urlencoded编码。所以-d参数中一定要进行urlencode编码转换
kency@kency-ThinkPad-T420s ~ $ curl -k -X POST -d 'username=xxx%40t2cloud.net&password=%40WSX3edc&verify_code=aaaa' -H "Content-type: application/x-www-form-urlencoded" https://192.168.104.100:8090/mental/auth/login/ | python -mjson.tool
{
    "code": 200,
    "data": {
        "current_project": {
            "id": "1c0cf20be4b14477b4819c60a1dc6563",
            "name": "xxx@t2cloud.net"
        },
        "projects": [
            {
                "current_project": true,
                "id": "1c0cf20be4b14477b4819c60a1dc6563",
                "name": "xxx@t2cloud.net"
            }
        ],
        "regions": [
            {
                "current_region": true,
                "group_by": "1",
                "id": "BJPOC-REGION1",
                "name": "BJPOC-REGION1",
                "type": "bfc"
            },
            {
                "current_region": false,
                "group_by": "1",
                "id": "BJPOC-REGION2",
                "name": "BJPOC-REGION2",
                "type": "baremetal"
            },
            {
                "current_region": false,
                "group_by": null,
                "id": "BJPOC-REGION3",
                "name": "BJPOC-REGION3",
                "type": null
            }
        ],
        "session_key": "6af344100d354535",
        "switcher_endpoints": {}
    },
    "message": "ok"
}

从上面可以看到,"session_key": "6af344100d354535",我们将其存放在USER_TOKEN的环境变量中:

$ export USER_TOKEN=gAAAAABZ3wTDWY

通过 curl 调用接口

调用API接口。

  • 如果是https接口一定要加 -k 参数, 跳过证书检查。
  • 接口URL及参数参看相应接口文档。
curl -k -X GET -H "X-Auth-Token:$USER_TOKEN" https://192.168.104.100:8080/portal/api/volumes?limit=1&offset=0 | python -mjson.tool
{
    "code": 0,
    "data": {
        "result": [
            {
                "attachments": [],
                "availability_zone": "default_availability_zone",
                "bootable": false,
                "created_at": "2017-10-27T06:30:18",
                "description": "",
                "host": null,
                "id": "1bb85126-f2a2-4f89-8ed9-a04a01ad24ae",
                "is_iso": false,
                "metadata": {},
                "multiattach": false,
                "name": "zhx-t1",
                "project": "zhangxu@t2cloud.net",
                "size": 1,
                "snapshot_id": "",
                "status": "available",
                "timesince": "34 \u5206\u949f",
                "volume_type": "ceph"
            }
        ],
        "total": 4
    },
    "message": null
}