解決Django的request.POST獲取不到內(nèi)容的問(wèn)題
我通過(guò)如下的一段程序發(fā)送post請(qǐng)求:
import urllib3 pool = urllib3.connection_from_url('http://127.0.0.1:8090') resp = pool.request('POST', '/polls/', fields={'key1':'value1', 'key2':'value2'}, headers={'Content-Type':'application/json'}, encode_multipart=False)
在服務(wù)器端我用request.POST期望能獲取到<QueryDict: {u'key2': [u'value2'], u'key1': [u'value1']}>,但是我發(fā)現(xiàn)獲取到的是一個(gè)空的<QueryDict: {}>,用reqyest.body是能獲取到原始的請(qǐng)求內(nèi)容key2=value2&key1=value1的。
這個(gè)時(shí)候只能去文檔中找答案了,但是貌似Django中的文檔也沒(méi)給出我答案,這時(shí)候我就只能通過(guò)源碼來(lái)找答案了,下面是class HttpRequest(object)中獲取POST QueryDict的函數(shù)部分:
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. # Mark that an error occurred. This allows self.__repr__ to # be explicit about it instead of simply representing an # empty POST 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()
函數(shù)看起來(lái)有點(diǎn)長(zhǎng),但是我們只要關(guān)注后面的if elif else這三個(gè)分支即可,從elif self.content_type == 'application/x-www-form-urlencoded':這個(gè)分支能看到只有請(qǐng)求header中的'Content-Type':'application/x-www-form-urlencoded'才會(huì)填充request.POST,其它情況下只有一個(gè)空的<QueryDict: {}>。
從這個(gè)問(wèn)題也看到了Django對(duì)'Content-Type':'application/json'沒(méi)有做任何處理,跟我預(yù)想的有一點(diǎn)不一樣。
以上這篇解決Django的request.POST獲取不到內(nèi)容的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用 Python 實(shí)現(xiàn)微信消息的一鍵已讀的思路代碼
利用python可以實(shí)現(xiàn)微信消息的一鍵已讀功能,怎么實(shí)現(xiàn)呢?你肯定會(huì)想著很復(fù)雜,但是python的好處就是很多人已經(jīng)把接口打包做好了,只需要調(diào)用即可,今天通過(guò)本文給大家分享使用 Python 實(shí)現(xiàn)微信消息的一鍵已讀的思路代碼,一起看看吧2021-06-06python爬蟲(chóng)開(kāi)發(fā)之Request模塊從安裝到詳細(xì)使用方法與實(shí)例全解
這篇文章主要介紹了python爬蟲(chóng)開(kāi)發(fā)之Request模塊從安裝到詳細(xì)使用方法與實(shí)例全解,需要的朋友可以參考下2020-03-03關(guān)于pandas.DataFrame的類(lèi)SQL操作
這篇文章主要介紹了關(guān)于pandas.DataFrame的類(lèi)SQL操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-08-08Python簡(jiǎn)潔優(yōu)雅的推導(dǎo)式示例詳解
這篇文章主要給大家介紹了關(guān)于Python簡(jiǎn)潔優(yōu)雅的推導(dǎo)式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04python實(shí)現(xiàn)kmp算法的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)kmp算法的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04pandas使用get_dummies進(jìn)行one-hot編碼的方法
今天小編就為大家分享一篇pandas使用get_dummies進(jìn)行one-hot編碼的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Python ORM框架SQLAlchemy學(xué)習(xí)筆記之關(guān)系映射實(shí)例
這篇文章主要介紹了Python ORM框架SQLAlchemy學(xué)習(xí)筆記之關(guān)系映射實(shí)例,Classic (經(jīng)典模式)和Modern (現(xiàn)代模式),分別介紹了,需要的朋友可以參考下2014-06-06