天天看點

python接口自動化之post請求coding:utf-8

request post請求詳解

post請求代碼如下:

coding:utf-8

#導入request包
import requests
#導入json包 用于将字典資料轉換為json格式的資料
import json
#設定請請求參數
params={'yoyo':'hello word'}
#将請求參數為字典類型的資料轉換為json格式的資料
data_json=json.dumps(params)
#設定請求頭  将請求的資料設定為json格式的資料
headers={"Content-Type": "application/json; charset=utf-8"}
#設定請求的url
url = 'http://httpbin.org/post'
#設定請求方式為post請求
postname=requests.post(url=url,json=data_json,headers=headers)
#列印響應内容
print('響應内容:',postname.text)
#列印響應狀态碼
print('響應狀态碼:',postname.status_code)
#列印請求頭資訊
print('請求頭資訊:',postname.request.headers)
#列印響應頭資訊
print('響應頭資訊:',postname.headers)

           

post請求響應結果如下:

python接口自動化之post請求coding:utf-8

request post請求注意事項:

(1)設定請求頭為json格式
(2)導入json包 使用json.dumps(參數名稱)轉換為json格式
(3)post傳入參數(url資訊,請求頭資訊,參數資訊)
           

繼續閱讀