DRF解析器源码分析
1 解析器
解析请求者发来的数据(JSON)
使用 request.data
获取请求体中的数据。
这个 reqeust.data
的数据怎么来的呢?其实在drf内部是由解析器,根据请求者传入的数据格式 + 请求头来进行处理。
drf默认的解析器三个JSONParser
, FormParser
, MultiPartParser
1.1 JSONParser (*)
只支持JSON格式的数据

1.2 FormParser

1.3 MultiPartParser(*)
既支持发送数据,又支持传输文件

解析器可以设置多个,默认解析器:
python
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import MultiPartParser, JSONParser, FormParser
class UserView(APIView):
parser_classes = [MultiPartParser, JSONParser, FormParser]
def post(self, request):
print(request.content_type)
print(request.data)
return Response("...")
1.5 源码分析
