I am attempting to send some binary data from a client via a blob to a django rest server, but the content of the received file ends up being "{}" independently of the content of the blob. I do not want to upload using base64 encoding.
sample client code:
fetch("myserver/upload/file", {
headers: {
'Authorization': 'Token mytoken',
'Content-Disposition': 'attachment; filename="filename.dat"'
},
method: "POST",
body: new Blob(Array.from(Array(10000).keys()))
);
On the server side I have a view class:
from rest_framework import generics
from rest_framework.parsers import FileUploadParser
from rest_framework.response import Response
class MyView(generics.GenericAPIView):
parser_classes = (FileUploadParser,)
def post(self, request):
f = request.data.get('file')
# some processing of file...
return Response(
{"response_info_key": "response_info_value"},
status = 201
)
What I observe independently of whether I send the post using jquery, fetch or xhr, and independently of blob content, what is received on server side is a file object whose only content is the two characters "{" and "}". I am required to add the Content-Disposition header, but not confident that this is the form it should have.
If I on the other hand set a "binary" body using Postman and the same headers, and specify file, the server successfully receives the file. How should I correct my POST request to make it work in a Javascript client?
Python version: 2.7.6
Django==1.11
requests==2.22.0
djangorestframework==3.6.2
EDIT: It seems like request header has 'HTTP_CONTENT_LENGTH': 2 on the server side, while the CONTENT-LENGTH on client side of the request is ~10^4