當有了請求之後,怎麼操作我們的返回呢?同樣的創建方法,同樣需要有一個flow,可以理解爲承上啓下的對象,一個上下文句柄,這樣我現在已經擁有了flow,那麼久可以通過flow.response獲取我們所有的返回數據,獲取到了我們就可以把數據打印出來,例如狀態碼,response_data.status_code ,還有我們的響應結果,response_data.text,這時就擁有了我們所有的數據,可以打印出來
def response(flow):
response_data = flow.response
print('code------------>',response_data.status_code)
print('res_data-------------------->',response_data.text)
發現很多亂碼的數據,我們先不管亂碼不亂碼,至少看到了這些數據,而且通過瀏覽器數據是擁有的,只是圖片而已
那麼怎麼處理亂碼,打開瀏覽器,查看請求,因爲我們返回的是圖片就是說現在的content-type是image,那能不能通過這個content-type進行判斷,如果不是我想要的,我就給你過濾掉,所以這時候就要從header下手,
def response(flow):
response_data = flow.response
response_header = response_data.headers
content_type = response_header['Content-Type']
print('content_type---------------->',content_type)
print('code------------>',response_data.status_code)
print('res_data-------------------->',response_data.text)
這裏發現,圖片格式—亂碼
所以我們要判斷,如果content-type中帶有image就是個圖片,咱們就不打印結果,else打印結果,來更新代碼,實現一下
def response(flow):
response_data = flow.response
response_header = response_data.headers
content_type = response_header['Content-Type']
if 'image' in content_type:
print('這裏返回的是圖片')
else:
print('content_type---------------->',content_type)
print('code------------>',response_data.status_code)
print('res_data-------------------->',response_data.text)
這裏我們的響應值看起來就乾淨了很多,現在就完成了一個結合,既可以拿到你的請求數據,也可以拿到你的返回數據,也可以說我監聽了整個app操作過程中所有數據的走向,我想怎麼去操作,就可以怎麼去操作,相對而言就輕鬆了很多,也可以這樣編碼,在格式爲appliction/json的情況下打印,否則格式不正確
def response(flow):
response_data = flow.response
response_header = response_data.headers
content_type = response_header['Content-Type']
if 'image' in content_type:
print('這裏返回的是圖片')
elif 'application/json' in content_type:
print('content_type---------------->', content_type)
print('code------------>', response_data.status_code)
print('res_data-------------------->', response_data.text)
else:
print('格式非預期')