Django解決無法從request.POST中獲取URL傳進來的參數(shù)
背景
上周做項目的時候,有個Post Webhook回調(diào)不僅在Body里傳了數(shù)據(jù),同時URL里也傳了參數(shù)。
request.POST這個QueryDict打印出來的值為{},無法獲取URL中的參數(shù)。
原因分析
這種問題,首先就是看代碼。
大概掃了下Request的代碼,問題出在如下的位置:
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()
這個回調(diào)接口傳進來的content_type 為 application/xml,所以會默認不解析。
其實按照這段代碼來看,只要是Post請求,都不會解析URL上的參數(shù)(我覺得這才是正常套路,接口回調(diào)不規(guī)范,他們要背鍋)。
問題解決
其實很簡單,我們只需要把帶參數(shù)的URL搞出來即可。
post = QueryDict(request.get_full_path().split('?')[1])
post.get('xx')或者
# 這段代碼是根據(jù)get_full_path的具體實現(xiàn)寫出來的,還沒測,提供個思路給大家
post = QueryDict(iri_to_uri(self.META.get('QUERY_STRING', '')))
post.get('xx')get_full_path具體實現(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 ''
)(遇事不決,看代碼就對了)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python+Qt身體特征識別人數(shù)統(tǒng)計源碼窗體程序(使用步驟)
這篇文章主要介紹了Python+Qt身體特征識別人數(shù)統(tǒng)計源碼窗體程序(使用步驟),本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
python入門:argparse淺析 nargs=''+''作用
這篇文章主要介紹了python入門:argparse淺析 nargs='+'作用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Pandas實現(xiàn)解析JSON數(shù)據(jù)與導(dǎo)出的示例詳解
其實使用pandas解析JSON?Dataset要方便得多,所以這篇文章主要為大家介紹了Pandas實現(xiàn)解析JSON數(shù)據(jù)與導(dǎo)出的具體方法,需要的小伙伴可以收藏一下2023-07-07
pyinstaller打包django項目的實現(xiàn)步驟
本文主要介紹了pyinstaller打包django項目的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
使用Python玩轉(zhuǎn)串口(基于pySerial問題)
這篇文章主要介紹了使用Python玩轉(zhuǎn)串口(基于pySerial問題),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
pandas把dataframe轉(zhuǎn)成Series,改變列中值的類型方法
下面小編就為大家分享一篇pandas把dataframe轉(zhuǎn)成Series,改變列中值的類型方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04

