呼叫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
- hicloud CaaS / CVPC API的相關資訊為
- 主機: hws.hicloud.hinet.net
- Port: 443
- HTTP protocol: HTTPS
- Action: /cboss-hws-web/api/hws/
- 先到 hicloud 用戶平台取得 Access Key, Secret key 的資訊,如:
- Access Key: U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0
- Secret key: WWpJNU16a3pOV1JsWWpNeU5HVXdOMkkxTURNd1lUbG1OMlEwTXpSaFptST0
- 到 開發者中心說明 > 雲運算 > 雲伺服器 > 歡迎使用 的右側 共通參數頁面中,分別準備下列參數
- chtAuthType=hwspass
- version=2013-03-29 (會依據不同版本而有所差別)
expires=2013-03-17T17%3A50%3A04Z (建議為目前系統時間再加上十五分鐘,此為 ISO 8601 時間格式,但在 URL 參數在傳遞中,會把一些特殊字元給替換掉,如 “:”, “+” …等,所以原本為 2013-03-17T17:50:04Z 就被替換成 2013-03-17T17%3A50%3A04Z)
- accessKey=U0U0MU5UQXhNREF3TVRFek5qSTVPRFkxTURneU1UWT0
- action=runInstances
- 參考 開發者中心說明 > 雲運算 > 雲伺服器 > 動作 > runInstances 中Request參數之說明,準備以下的參數
- imageId=hi-olajtpss
- instanceType=HC1.S.LINUX
- monitoringEnabled=false
- instanceName=myInstance
- count=1
- 傳遞的Request參數值需經URL編碼 (URL encoding)
- 範例
原始值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 的格式 - 參考資料
- 維基百科 - 百分號編碼
(http://zh.wikipedia.org/zh-tw/%E7%99%BE%E5%88%86%E8%99%9F%E7%B7%A8%E7%A2%BC) - URL Encode and Decode Tool (http://www.url-encode-decode.com/)
- 維基百科 - UTF-8 (http://zh.wikipedia.org/wiki/UTF-8)
- 維基百科 - 百分號編碼
- 範例
- 有了以上的資訊,可產生出以下的 url
- 依據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
- 透過 REST 的工具來執行此動作,以 Firefox + RESTClient 為例
- 在 Method 中填入 GET
- URL 中填入帶有 Singature 的網址
- 按下 “SEND”
- 在 Response 區域可獲得執行的結果
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