When uploading a file, if I send a file upload request to Postman, the request goes through, but no data seems to be sent.
Do I need to use another method to receive the file in form-data format? Or did I do something wrong?
file = request.FILES.get('file')
I thought there might be a problem in this part, but there seems to be no problem.
I don’t know what the problem is
terminal
<rest_framework.request.Request: POST '/upload/'> {} None
Bad Request: /upload/
[07/May/2024 01:07:48] "POST /upload/ HTTP/1.1" 400 53
[07/May/2024 01:07:48] code 400, message Bad request syntax ('----------------------------628614084899426196724366')
[07/May/2024 01:07:48] "----------------------------628614084899426196724366" 400 -
code
class UploadView(APIView):
s3_client = boto3.client(
's3',
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY
)
def post(self, request: object) -> Response:
try:
file = request.FILES.get('file')
print(request, request.data, file)
self.s3_client.upload_fileobj(
file.name,
settings.AWS_BUCKET_NAME,
request.user+timezone.now().strftime('%Y%m%d%H%M%S'), # add uuid
ExtraArgs={
"ContentType": file.content_type
}
)
except Exception as e:
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_201_CREATED)
postman
postman image
I uploaded the image file in form-data format and hope it uploads normally.
Issues related to s3 will be resolved later.