天天看點

Python-使用requests進行接口測試-發送POST請求

Python-使用requests進行接口測試-發送POST請求:

(一)與GET請求的差別:

1)調用request.post(url=請求的url,data=請求的參數);

2)POST請求的參數,使用data傳值。(GET請求,是使用params=請求的參數);

下面以調用通用的百度翻譯API請求為例:

可以在http://api.fanyi.baidu.com/doc/21裡查找接口位址。

步驟:

1)從接口文檔中,我們可以查找到我們所需要的請求url位址和所需的請求傳遞參數:q,from,to,appid,salt,sign

Python-使用requests進行接口測試-發送POST請求

2)檢視接口文檔的例子,準備好請求的傳遞參數;

Python-使用requests進行接口測試-發送POST請求

傳遞參數:

①q:you are an apple in my eyes (可以替換為您所要翻譯的内容)

②from:auto

Python-使用requests進行接口測試-發送POST請求

③to:zh(輸入你所要翻譯的目智語言代碼)

④appid:需注冊成為開發者,才可以獲得

⑤salt:9527(随機碼)

⑥sign:appid+q+salt+密鑰 的MD5值

a. 首先獲得拼接後的字元串str = xxx(appid)you are an apple in my eyes9527xxx(密鑰);

b. 将str轉成32位小寫的MD5;

MD5加密網址:https://md5jiami.bmcx.com/?_t=1552612737

Python-使用requests進行接口測試-發送POST請求

2)回到Pycharm中,開始寫腳本。

Python-使用requests進行接口測試-發送POST請求

3)運作,檢視結果。

Python-使用requests進行接口測試-發送POST請求