【问题标题】:is it possible to pass request.body via post request in django test client?是否可以通过 django 测试客户端中的 post 请求传递 request.body?
【发布时间】:2015-11-22 09:01:56
【问题描述】:

到目前为止,我还没有找到任何方法。

https://docs.djangoproject.com/en/1.8/topics/testing/tools/#django.test.Client.options 

显示 options 允许 request.body 通过 get 请求,但找不到任何通过 post 请求传递的方法。任何想法,我一直在寻找几个小时。

【问题讨论】:

  • 我不确定我是否理解这个问题。你想在测试发布请求中发送什么数据?

标签: python django unit-testing django-testing


【解决方案1】:

是的:

self.client.generic('POST', '/url', 'raw post data')

【讨论】:

    【解决方案2】:

    如果你真的想做:

    request.body = b'{"first": "fred", "last": "dredd"}'
    

    你应该这样做:

    request._body = b'{"first": "fred", "last": "dredd"}'
    

    【讨论】:

      猜你喜欢
      • 2019-04-20
      • 2018-03-09
      • 1970-01-01
      • 2016-11-26
      • 2019-08-07
      • 2021-11-01
      • 2014-02-15
      • 2018-09-08
      • 2021-05-16
      相关资源
      最近更新 更多