Django解決無(wú)法從request.POST中獲取URL傳進(jìn)來(lái)的參數(shù)
背景
上周做項(xiàng)目的時(shí)候,有個(gè)Post Webhook回調(diào)不僅在Body里傳了數(shù)據(jù),同時(shí)URL里也傳了參數(shù)。
request.POST這個(gè)QueryDict打印出來(lái)的值為{},無(wú)法獲取URL中的參數(shù)。
原因分析
這種問(wèn)題,首先就是看代碼。
大概掃了下Request的代碼,問(wèn)題出在如下的位置:
def _load_post_and_files(self): """Populate self._post and self._files if the content-type is a form type""" if self.method != 'POST': self._post, self._files = QueryDict(encoding=self._encoding), MultiValueDict() return if self._read_started and not hasattr(self, '_body'): self._mark_post_parse_error() return if self.content_type == 'multipart/form-data': if hasattr(self, '_body'): # Use already read data data = BytesIO(self._body) else: data = self try: self._post, self._files = self.parse_file_upload(self.META, data) except MultiPartParserError: # An error occurred while parsing POST data. Since when # formatting the error the request handler might access # self.POST, set self._post and self._file to prevent # attempts to parse POST data again. self._mark_post_parse_error() raise elif self.content_type == 'application/x-www-form-urlencoded': self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict() else: self._post, self._files = QueryDict(encoding=self._encoding), MultiValueDict()
這個(gè)回調(diào)接口傳進(jìn)來(lái)的content_type 為 application/xml,所以會(huì)默認(rèn)不解析。
其實(shí)按照這段代碼來(lái)看,只要是Post請(qǐng)求,都不會(huì)解析URL上的參數(shù)(我覺(jué)得這才是正常套路,接口回調(diào)不規(guī)范,他們要背鍋)。
問(wèn)題解決
其實(shí)很簡(jiǎn)單,我們只需要把帶參數(shù)的URL搞出來(lái)即可。
post = QueryDict(request.get_full_path().split('?')[1]) post.get('xx')
或者
# 這段代碼是根據(jù)get_full_path的具體實(shí)現(xiàn)寫出來(lái)的,還沒(méi)測(cè),提供個(gè)思路給大家 post = QueryDict(iri_to_uri(self.META.get('QUERY_STRING', ''))) post.get('xx')
get_full_path具體實(shí)現(xiàn)如下:
def get_full_path(self, force_append_slash=False): return self._get_full_path(self.path, force_append_slash) def _get_full_path(self, path, force_append_slash): # RFC 3986 requires query string arguments to be in the ASCII range. # Rather than crash if this doesn't happen, we encode defensively. return '%s%s%s' % ( escape_uri_path(path), '/' if force_append_slash and not path.endswith('/') else '', ('?' + iri_to_uri(self.META.get('QUERY_STRING', ''))) if self.META.get('QUERY_STRING', '') else '' )
(遇事不決,看代碼就對(duì)了)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python+Qt身體特征識(shí)別人數(shù)統(tǒng)計(jì)源碼窗體程序(使用步驟)
這篇文章主要介紹了Python+Qt身體特征識(shí)別人數(shù)統(tǒng)計(jì)源碼窗體程序(使用步驟),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12python入門:argparse淺析 nargs=''+''作用
這篇文章主要介紹了python入門:argparse淺析 nargs='+'作用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Pandas實(shí)現(xiàn)解析JSON數(shù)據(jù)與導(dǎo)出的示例詳解
其實(shí)使用pandas解析JSON?Dataset要方便得多,所以這篇文章主要為大家介紹了Pandas實(shí)現(xiàn)解析JSON數(shù)據(jù)與導(dǎo)出的具體方法,需要的小伙伴可以收藏一下2023-07-07pyinstaller打包django項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了pyinstaller打包django項(xiàng)目的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09使用Python玩轉(zhuǎn)串口(基于pySerial問(wèn)題)
這篇文章主要介紹了使用Python玩轉(zhuǎn)串口(基于pySerial問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09pandas把dataframe轉(zhuǎn)成Series,改變列中值的類型方法
下面小編就為大家分享一篇pandas把dataframe轉(zhuǎn)成Series,改變列中值的類型方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04