Python發送http請求時遇到問題總結


1、報錯信息為“ERROR 'str' object has no attribute 'endwith'”,排查發現endswith方法名寫錯了,少了s,寫成了 'endwith'

                if interface_url.endswith('?'):
                    req_url = interface_url + temp_interface_param
                else:
                    req_url = interface_url + '?' + temp_interface_param

2、報錯信息為“ERROR request() got an unexpected keyword argument 'header”,排查發現將參數名headers寫成了header

response = requests.get(url=req_url, headers=header_data, verify=False, timeout=10)

3、報錯信息為“ERROR No connection adapters were found for 'test.XXX.xxx/xxx/'”,排查發現URL少了“http://”,加上解決問題

4、發送HTTP請求,請求方式是post時,url和參數等都正確,但是請求時還是報錯,排查發現使用Python發送請求時,並沒有把參數傳進去,導致報錯;

進一步排查發現,請求頭參數類型未設置content-type值,常用的content-type有四種,如下:

(1)application/x-www-form-urlencoded:是最常見的數據類型,通常表明請求的數據類型是鍵值對類型

(2)application/json:請求的數據類型是json格式

(3)multipart/form-data:通常用於上傳文件

(4)application/xml:數據格式為xml格式

在請求頭中添加了'content-type':'application/x-www-form-urlencoded'數據后,傳參也遵循對應的格式,HTTP請求就能正常返回數據了

請求頭content-type值參考:https://www.jianshu.com/p/f59b04ffea61


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM