呼叫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 區域可獲得執行的結果