Mazhar Abbas
Mazhar Abbas

Reputation: 157

Transferring django binaryfiled data via post request

I am working with django rest framework and aws S3 storage. I am uploading video files to S3 bucket and saving the encryption key in django models' binaryfield format (a requirement of s3 storage). Now i need to pass this key to another remote django server using post request. When i pass the key using requests library, somehow it is changed when i extract it from post request on the other server and i am unable to use it. It would be highly appreciated if some tells me how to pass binaryfield data via post request using django requests library. My code for making post request is

import requests enc_key = some_object.key # key is a models.BinaryField() data = {'enc_key' : enc_key} response = requests.post(url, data = data)

while on the other server i extract enc_key from post request like this

enc_key = request.POST['enc_key']

but this key is not usable anymore.

Upvotes: 1

Views: 304

Answers (1)

Sergei Zherevchuk
Sergei Zherevchuk

Reputation: 602

Does enc_key really an instance of models.BinaryField? In case of python3 it should be memoryview.

Btw, requests library is totally separate from Django, you should try to post binary data without Django at all and then fight with framework if necessary. Maybe you can retrieve data from database directly from remote machine? If not, try to send test binary data in that way:

res = requests.post(url=remote_url,
                    data=data,
                    headers={'Content-Type': 'application/octet-stream'})

Upvotes: 1

Related Questions