Django-react native:请求失败,状态码为 400

时间:2021-05-25 13:04:22

标签: android django react-native mobile axios

我正在尝试使用 android 模拟器从 react-native 前端向我的 api 发布图像,但我收到此错误:请求失败,状态代码为 400

这是我的 axios 请求:

useEffect(() => {
  const uploaded = new FormData()
  uploaded.append('image_file',image)
  axios.post('http://10.0.2.2:8000/upload/', uploaded)
      .then(response => console.log(response));
}, []);

这是我的 Views.py :

def methodes(self,request,*args,**kwargs):
        if request.method=='POST':
            image_file=request.data['image_file']
            #title=request.data['title']
            Upload.objects.create(image_file=image_file)
            serializer=ImageSerializer(data=request.FILES['image'], required=False)
            if serializer.is_valid():
               serializer.save()
               return HttpResponse({'message':'ok'},status=200) 
            return HttpResponse({'message':'error'},status=400)

0 个答案:

没有答案