首页 > 解决方案 > 为什么 Django HttpRequest (POST) 似乎将 request.data 设置为 request.body 中“data”键的值?

问题描述

背景

我有一个 Django wsgi 应用程序,它也使用 django-rest-framework。向我的应用程序发出一个 POST 请求,该应用程序具有一个包含关键“数据”的 JSON 有效负载,例如。

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

我注意到 WSGIRequest 对象(一旦它被初始化,即不是 django-rest-framework 请求对象)有 request.body 包含完整的有效负载,但 request.data 包含 request.body[' 的值数据']。我的应用程序被编写为依赖于 request.data 中仅存在“数据”键值这一事实。

我最近将 Django 从 1.6 升级到 1.11,并将 django-rest-framework 从 2.3.8 升级到 3.8.2。较新版本的 DRF 实现了自己的 request.data ,而之前它只是将其代理到底层 Django 请求对象。新实现将 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"}}}' 

标签: pythondjangodjango-rest-frameworkhttp-postwsgi

解决方案


我已经回答了我自己的问题。我发现了一个将 request.data 设置为 request.body['data'] 的中间件。新版本的 DRF 在这里有一个命名空间冲突,因为它也设置了 request.data,但是这发生在 Django 中间件生效之后。为了解决这个问题,我在访问 data 属性后覆盖了 initialize_request 并手动设置了 _full_data 。不幸的是,更改我的应用程序使用 request.data 的每种情况是不切实际的。


推荐阅读