调用REST的方法


上次更新时间: 2017/03/22


以下为一hicloud CaaS / CVPC API调用的实际示例 (以 runInstances 为例):

 

    https://hws.hicloud.hinet.net/cloud_hws/api/hws/?action=runInstances&version=2013-03-29
    &chtAuthType=hwspass&imageId=hi-olajtpss&instanceType=HC1.S.LINUX&monitoringEnabled=false
    &instanceName=myInstance&count=1&accessKey=U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0
    &expires=2013-03-13T17%3A50%3A04%Z&signature=VGMS-tWF3Xa2ixBhwvzgi7Aplhc
    

 

  1. hicloud CaaS / CVPC API的相关信息为
    1. 主机: hws.hicloud.hinet.net
    2. Port: 443
    3. HTTP protocol: HTTPS
    4. Action: /cboss-hws-web/api/hws/
  2. 先到 hicloud 用户平台取得 Access Key, Secret key的信息,如:
    1. Access Key: U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0
    2. Secret key: WWpJNU16a3pOV1JsWWpNeU5HVXdOMkkxTURNd1lUbG1OMlEwTXpSaFptST0
  3. 到 开发者中心说明 > 云运算 > 云服务器 > 欢迎使用的右侧 共通参数页面中,分别准备下列参数
    1. chtAuthType=hwspass
    2. version=2013-03-29 (会因不同版本而有所差别)
    3. expires=2013-03-17T17%3A50%3A04Z (建议为目前系统时间再加上十五分钟,此为 ISO 8601 时间格式,但在 URL 参数在传递中,会把一些特殊字符给替换掉,如 “:”, “+” …等,所以原本为 2013-03-17T17:50:04Z 就被替换成 2013-03-17T17%3A50%3A04Z)

    4. accessKey=U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0
    5. action=runInstances
  4. 参考 开发者中心说明 > 云运算 > 云服务器 > 动作 > runInstances 中Request参数之说明,准备以下的参数
    1. imageId=hi-olajtpss
    2. instanceType=HC1.S.LINUX
    3. monitoringEnabled=false
    4. instanceName=myInstance
    5. count=1
  5. 传入的Request参数值需经URL编码 (URL encoding)
    1. 示例
      原始值
      URL Encode
      说明
      noun phrase noun%20phrase 有空白
      vm!@#$%^&*()-=_+[]{};':",./<>?999 vm!%40%23%24%25%5E%26*()-%
      3D_%2B%5B%5D%7B%7D%3B%27
      %3A%22%2C.%2F%3C%3E%3F999
      %E4%B8%AD%E6%96%87%E5%AD%97 多国语系的字要使用UTF-8 的格式
    2. 参考数据
      1. 维基百科 - 百分号编码
        (http://zh.wikipedia.org/zh-tw/%E7%99%BE%E5%88%86%E8%99%9F%E7%B7%A8%E7%A2%BC)
      2. URL Encode and Decode Tool (http://www.url-encode-decode.com/)
      3. 维基百科 - UTF-8 (http://zh.wikipedia.org/wiki/UTF-8)
  6. 有了以上的信息,可生成以下的 url
  7.   https://hws.hicloud.hinet.net/cloud_hws/api/hws/?action=runInstances&version=2013-03-29&
      chtAuthType=hwspass&imageId=hi-olajtpss&instanceType=HC1.S.LINUX&monitoringEnabled=false&
      instanceName=myInstance&count=1&accessKey=U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0&
      expires=2013-03-13T17%3A50%3A04%Z
  8. 依据Signature程序案例,配合 Secret key 与上一步骤的 url,可生成带有 Singature 的网址
      https://hws.hicloud.hinet.net/cloud_hws/api/hws/?action=runInstances&version=2013-03-29&chtAuthType=hwspass&
      imageId=hi-olajtpss&instanceType=HC1.S.LINUX&monitoringEnabled=false&instanceName=myInstance&count=1&
      accessKey=U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0&expires=2013-03-13T17%3A50%3A04%Z&
      signature=VGMS-tWF3Xa2ixBhwvzgi7Aplhc 
  9. 通过 REST 的工具来执行此动作,以 Firefox + RESTClient 为例
    1. 在 Method 中填入 GET
    2. URL 中填入带有 Singature 的网址
    3. 按下 “SEND”
    4. 在 Response 区域可获得执行的结果