0

I am trying to send image to the django server. Here is my code:

@csrf_exempt
def classify_image(request):
    if request.method == 'POST' and request.FILES:
        try:
            image = request.FILES['file']
            if image:
                img_b64 = b64encode(image.read())
                res = main.classify_image(img_b64)
                return JsonResponse({
                    "status": "ok",
                    "result": {
                        "class": res[0],
                        "confidence": res[1],
                    }
                })
        except Exception as e:
            return JsonResponse({
                "status": "error",
                "error": str(e)
            })

    return JsonResponse({
        "status": "error",
        "error": "Image file is required"
    })

But I keep on getting Error image

I am sending requesting using postman, here's the sample postman 1 postman 2

The weird thing is the code was working perfectly fine some hours ago. Later I didn't change a single thing but it was throwing error.

  • 1
    Does this help? Check the comments in the accepted answer: https://stackoverflow.com/questions/32017119/put-request-for-image-upload-not-working-in-django-rest – lummers Sep 03 '22 at 18:43
  • Nope! Now it says { "status": "error", "error": "Image file is required" } – chirag goyal Sep 03 '22 at 18:54
  • OKk.. I have solved one issue, the postman request is not working now but was working before is because the image I am sending with postman is now null as I restarted postman after hours and it tried to fetch it from the postman's server but the image is only in local machine. But the issue is now, while sending request using fetch api (my frontend is in nextjs), how do is specify the boundary – chirag goyal Sep 03 '22 at 19:01
  • """Check the comments in the accepted answer: stackoverflow.com/questions/32017119/…""" Now this worked, thanks a lot – chirag goyal Sep 03 '22 at 19:09

0 Answers0