淺談python3發(fā)送post請求參數(shù)為空的情況
post請求的時候如果不帶參數(shù),其實(shí)作用就跟get請求一樣。我們在做接口測試的時候,發(fā)現(xiàn)開發(fā)就全部使用的post,get的作用就被這樣的post空參數(shù)請求給替代了。
在Python代碼請求,如下:
class HttpHelper():
def __init__(self):
'''獲取driver對象,和接口ip地址信息,里面的方法大家可以忽略,根據(jù)自己的情況來設(shè)置
'''
self.dr=Common.driver
run_info=Common().get_current_run_config()
app_info=Common().get_app_config()[run_info['_envir']]
self.ip=app_info['url'].split('/')[2]
def post(self,module,interface_name,post_para={}):
'''arg: module 模塊名
interface_name 接口名稱
post_para 請求參數(shù),默認(rèn)是空字典,如果不填這個參數(shù)就是post請求參數(shù)為空的情況
'''
inter_info=Common().get_interface_info()[module]
url='http://'+self.ip+inter_info[interface_name]['url']
Common().logger_info("request - api - "+url)
postdata = bytes(urllib.parse.urlencode(post_para), encoding='utf8')
Common().logger_info("request - arg - "+str(post_para))
_jid=Common().get_jsessionid(self.dr) #獲取sessionid,這個方法是通過selenium的get_cookie方法來獲取sessionid,大家可以參考我其他的文章
header={
'Accept':'application/json, text/plain, */*',
'Connection': 'keep-alive',
'Content-Type':'application/x-www-form-urlencoded',
'Cookie':'JSESSIONID='+_jid+'',
'Host': ''+self.ip+'',
'Origin': 'http://'+self.ip+''
}
Common().logger_info("[header] - "+str(header))
try:
req=urllib.request.Request(url,postdata,header)
with urllib.request.urlopen(req) as resp:
response=resp.read().decode('utf-8')
response=json.loads(response)
Common().logger_info('response - '+str(response))
if response['data']!='':
Common().logger_info('http post success!!!')
return response
except Exception as e:
Common().logger_error(str(e))
代碼里的Common().logger_***是我們項(xiàng)目的日志方法,輸出一些執(zhí)行日志,大家可以忽略。
以上這篇淺談python3發(fā)送post請求參數(shù)為空的情況就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python實(shí)現(xiàn)簡單的數(shù)據(jù)備份
數(shù)據(jù)備份,即數(shù)據(jù)的復(fù)制和存儲,是指將數(shù)據(jù)從一個位置復(fù)制到另一個位置,以防止原始數(shù)據(jù)丟失或損壞,下面我們就來了解一下用Python如何實(shí)現(xiàn)這一功能吧2025-03-03
編寫Python腳本來獲取Google搜索結(jié)果的示例
這篇文章主要介紹了編寫Python腳本來獲取Google搜索結(jié)果的示例,也是利用Python編寫爬蟲的一個簡單實(shí)現(xiàn),需要的朋友可以參考下2015-05-05
在python中實(shí)現(xiàn)同行輸入/接收多個數(shù)據(jù)的示例
今天小編就為大家分享一篇在python中實(shí)現(xiàn)同行輸入/接收多個數(shù)據(jù)的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
用python實(shí)現(xiàn)一個簡單的驗(yàn)證碼
這篇文章主要介紹了用python實(shí)現(xiàn)一個簡單的驗(yàn)證碼的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯解決
這篇文章主要介紹了python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

