在django documentation中,它说:
HttpRequest.POST
A dictionary-like object containing all given HTTP POST parameters, providing that the request contains form data. See the QueryDict documentation below. If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead.
但是,服务器不响应浏览器(例如使用JS框架或表单),而是响应Anroid/iOS应用程序发送的restapi。在
如果客户机直接在POST请求中发送字段,我如何读取数据?例如,这个(Java+Unirest):
Unirest.post("/path/to/server")
.field("field1", "value2")
.field("field2", "value2");
编辑:我可以简单地使用response.POST["field1"]
读取数据,还是必须使用request.body
进行操作?在
编辑2:这样我就可以简单地使用request.body
作为类似于request.POST
的类似字典的对象?在
据我所知,Unirest的field方法只使用普通的
application/x-www-form-urlencoded
数据,就像HTML表单一样。所以您应该能够像您建议的那样使用response.POST["field1"]
。在从docs:
示例-来自
create
方法(viewsets):编辑1:在版本3(最新)中,
^{pr2}$request.DATA
已替换为request.data
:如果与之交互的api是基于sipmle Django类的视图,则可以通过
request.body
访问数据,如下所示:如果您使用的是Django rest framework api,则可以通过
^{pr2}$request.data
访问数据:NB:如果您发现
request.DATA
在互联网上的某个地方使用也是正确的,但它只对旧版本的DRF有效,并且它被弃用,取而代之的是新版本中的request.data
。在相关问题 更多 >
编程相关推荐