Ninja POST 422 (Unprocessable Entity) [duplicate]

Does anyone know why this happened? I can upload the picture just fine in swagger UI but I always got 422 http response.

    @http_post("/profile", response=dict)
    def upload_profile_image(self, request, file: UploadedFile = File(...)):
        """Upload profile image for business."""
        
        try:
            business = Business.objects.get(user=request.user)
        except Business.DoesNotExist:
            return JsonResponse({"msg": "You don't have business yet."}, status=404)
        business.image.save(file.name, ContentFile(file.read()), save=True)

        image_url = request.build_absolute_uri(business.image.url)  # Full URL
        print('put URL', image_url)
        return {
            "msg": f"{file.name} uploaded.",
            "business": image_url
        }

Front end (Next.js)

 const handleSubmit = async () => {
    
    const formData = new FormData();
    if (selectedFile) {
        formData.append('profile_image', selectedFile); // Match field name expected in backend
      }
    try {
      const response = await fetch(`http://127.0.0.1:8000/api/business/profile`, {
        method: "POST",
        body: formData,

      });

      if (!response.ok) {
        console.log("Failed to save edited business");
        return;
      }

    } catch (error) {
      console.log("Error saving edited queue:", error);
    }
  };

I even tried upload the same image that works on swagger but it doesn't too and I don't think it's because the image size.


Error: "POST - BusinessController[upload_profile_image] /api/business/profile" ([{'type': 'missing', 'loc': ('file', 'file'), 'msg': 'Field required'}],) Unprocessable Entity: /api/business/profile [13/Nov/2024 22:18:17] "POST /api/business/profile HTTP/1.1" 422 83
Back to Top