0

formdata not getting populated [working postman test] (https://i.stack.imgur.com/GFMae.png)

1>here is the react code

    formdata.append("file", selectedFile);
    console.log(formdata)
    fetch(`http://127.0.0.1:8000/file/upload`, {
      method: "POST",
      headers: {
        "Content-Type": "multipart/form-data"
      },
      body: formdata
    })
    console.log(formdata)
  };
  const handleFileSelect = (event) => {
    setSelectedFile(event.target.files[0])
  }

2>here is the django rest framework views code

  parser_classes = (MultiPartParser, FormParser)
  def post(self, request, *args, **kwargs):
    file_serializer = FileSerializer(data=request.data)
    if file_serializer.is_valid():
      file_serializer.save()
      return Response(file_serializer.data, status=status.HTTP_201_CREATED)
    else:
      return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

I have tried using the axios api with formdata but its not working as well

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
  • You are sending form data wrong https://stackoverflow.com/questions/47630163/axios-post-request-to-send-form-data – Konrad Mar 09 '23 at 14:14

0 Answers0