簡體   English   中英

為什么出現Django HttpRequest(POST)來將request.data設置為request.body中“ data”鍵的值?

[英]Why does a Django HttpRequest (POST) appear to set request.data to the value of the “data” key from request.body?

背景

我有一個Django wsgi應用程序,它也利用了django-rest-framework。 向我的應用程序發出了POST請求,該請求具有一個包含密鑰“數據”的JSON有效負載,例如。

{'meta': {'some': 'stuff'}, 'data': {'other': 'stuff'}}

我注意到WSGIRequest對象(一旦它被初始化,即還沒有django-rest-framework請求對象)就具有包含完整負載的request.body,但是包含request.body ['值的request.data。數據']。 我的應用程序被編寫為取決於在request.data中僅存在“數據”鍵值的事實。

我最近將Django從1.6升級到1.11,並將django-rest-framework從2.3.8升級到3.8.2。 較新版本的DRF實現了自己的request.data,而在此之前它只是將其代理到底層Django request對象。 新的實現將request.data設置為已解析的request.body。 這使我的應用程序進一步中斷。

我在源代碼或其他地方找不到任何指示,即request.data應該與request.body不同。 相反,似乎有幾個問題是相同的,這是理所當然的。 有人可以幫我弄清楚我是否發現了錯誤或預期的行為? 我的應用程序是否做出了不必要的假設?

謝謝

更新資料

這是一個郵遞員請求示例:

POST /api/data/ProvisioningWorkflow/5aeaf9caa9d1cb000887184f/execute/?hierarchy=1c0ffee2c0deab00da595101&nowait=true&format=json HTTP/1.1
Host: localhost
Content-Type: application/json
Accept: application/json
Authorization: Basic c3lzYWRtaW46c3lzYWRtaW4=
Cache-Control: no-cache
Postman-Token: 5140e8db-6ffa-e2d2-9791-698030fe6caf

{"data":{"name":"_test","parameters":{"parallel":"false","max_workers":"1"},"rollback_disabled":false,"workflow":[{"method":"add","entity_type":"model","entity":"data/Countries","templates":[{"template":"_test"}],"advanced_find_search_direction":"full_tree"}]},"request_meta":{},"meta":{"references":{"form_href":"/api/data/ProvisioningWorkflow/5aeaf9caa9d1cb000887184f/?hierarchy=1c0ffee2c0deab00da595101"}}}

如果我在wsgi.py中進行調試,則會看到以下問題:

>>> request.body                                                                                                                              
'{"data":{"name":"_test","parameters":{"parallel":"false","max_workers":"1"},"rollback_disabled":false,"workflow":[{"method":"add","entity_ty 
pe":"model","entity":"data/Countries","templates":[{"template":"_test"}],"advanced_find_search_direction":"full_tree"}]},"request_meta":{},"m 
eta":{"references":{"form_href":"/api/data/ProvisioningWorkflow/5aeaf9caa9d1cb000887184f/?hierarchy=1c0ffee2c0deab00da595101"}}}'             
>>> request.data                                                                                                                              
{u'rollback_disabled': False, u'name': u'_test', u'parameters': {u'max_workers': u'1', u'parallel': u'false'}, u'workflow': [{u'templates':   
[{u'template': u'_test'}], u'entity': u'data/Countries', u'method': u'add', u'advanced_find_search_direction': u'full_tree', u'entity_type':  
u'model'}]} 

我可以看到底層的請求對象,這里的行為再次變得很清楚:

>>> request._request.data                                                                                                                     
{u'rollback_disabled': False, u'name': u'_test', u'parameters': {u'max_workers': u'1', u'parallel': u'false'}, u'workflow': [{u'templates':   
[{u'template': u'_test'}], u'entity': u'data/Countries', u'method': u'add', u'advanced_find_search_direction': u'full_tree', u'entity_type':  
u'model'}]}                                                                                                                                   
>>> request.data                                                                                                                              
{u'request_meta': {}, u'meta': {u'references': {u'form_href':                                                                                 
u'/api/data/ProvisioningWorkflow/5aeaf9caa9d1cb000887184f/?hierarchy=1c0ffee2c0deab00da595101'}}, u'data': {u'rollback_disabled': False,      
u'name': u'_test', u'parameters': {u'max_workers': u'1', u'parallel': u'false'}, u'workflow': [{u'templates': [{u'template': u'_test'}],      
u'entity': u'data/Countries', u'method': u'add', u'advanced_find_search_direction': u'full_tree', u'entity_type': u'model'}]}}                
>>> request.body                                                                                                                              
'{"data":{"name":"_test","parameters":{"parallel":"false","max_workers":"1"},"rollback_disabled":false,"workflow":[{"method":"add","entity_ty 
pe":"model","entity":"data/Countries","templates":[{"template":"_test"}],"advanced_find_search_direction":"full_tree"}]},"request_meta":{},"m 
eta":{"references":{"form_href":"/api/data/ProvisioningWorkflow/5aeaf9caa9d1cb000887184f/?hierarchy=1c0ffee2c0deab00da595101"}}}'             
>>> request._request.body                                                                                                                     
'{"data":{"name":"_test","parameters":{"parallel":"false","max_workers":"1"},"rollback_disabled":false,"workflow":[{"method":"add","entity_ty 
pe":"model","entity":"data/Countries","templates":[{"template":"_test"}],"advanced_find_search_direction":"full_tree"}]},"request_meta":{},"m 
eta":{"references":{"form_href":"/api/data/ProvisioningWorkflow/5aeaf9caa9d1cb000887184f/?hierarchy=1c0ffee2c0deab00da595101"}}}' 

我已經回答了我自己的問題。 我發現了一個中間件,它正在將request.data設置為request.body ['data']。 DRF的新版本在此處存在名稱空間沖突,因為它還會設置request.data,但這是在Django中間件生效之后發生的。 我覆蓋了initialize_request並在訪問data屬性后手動設置了_full_data來解決此問題。 不幸的是,更改我的應用程序使用request.data的每種情況都是不切實際的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM