django:request.body、request.POST、request.data

request.POST与request.body:

  django中的request.POST只能取到Content-Type(请求头)为application/x-www-form-urlencoded(form表单默认格式)的数据,如果请求头为application/json(json格式),multipart/form-data(文件)等格式无法取到,只有在request.body里面能取到原生的数据。当发送过来的是JSON数据是,request.POST取到的数据是空的,这时只有用request.body取,再反序列化才能使用。nyo

#post请求中需要传递的数据
info= json.dumps({'a':{'id':1,'idc':'北京'},},ensure_ascii=flase) #支持中文
data = info 
#如果想传字典,要转为json字符串序列化。request.post找不到,要从request.body里看看

img

HTTP协议:

  请求首行:

    请求方式、url路径、HTTP协议版本

  请求头:

    常见的请求头有:

    Content-Type:文本的数据类型

    User-Agent:产生请求的浏览器类型

    Host:发送请求的主机名

    Accept:返回的数据格式

    Server、Cookie等等。

  请求体:

常见的请求方式get、post、put、delete

原文地址:https://www.cnblogs.com/hanfe1/p/13041725.html