Stop django from automatic unicodifing post

I am loading some data into a django view. Customer:

from poster.encode import multipart_encode def upload_data(upload_url, data, filename): print "Uploading %d bytes to server, file=%s..." % (len(data), filename) datagen, headers = multipart_encode({filename: data}) request = urllib2.Request(upload_url, datagen, headers) # Actually do the request, and get the response try: resp_f = urllib2.urlopen(request, timeout=120) except urllib2.URLError: return None res = resp_f.read() resp_f.close() return res #... def foo(self, event_dicts_td): event_dicts_td_json = json.dumps(event_dicts_td) res = upload_data(self.upload_url, event_dicts_td_json.encode('utf8').encode('zlib'), "event_dicts_td.json.gz") 

View:

 def my_view(request): event_dicts_td_json_gz = request.POST.get('event_dicts_td.json.gz') if not event_dicts_td_json_gz: return HttpResponse("fail") print type(event_dicts_td_json_gz), repr(event_dicts_td_json_gz[:10]) event_dicts_td_json_gz = event_dicts_td_json_gz.encode("utf8") print type(event_dicts_td_json_gz), repr(event_dicts_td_json_gz[:10]) event_dicts_td_json = event_dicts_td_json_gz.decode("zlib").decode("utf8") return HttpResponse("it still failed") 

Output:

 <type 'unicode'> u'x\ufffd\ufffd]s\ufffd\u0192\ufffd\ufffd\n' <type 'str'> 'x\xef\xbf\xbd\xef\xbf\xbd]s\xef' 

This is unacceptable. I just need raw bytes. I do not load unicode - I load raw bytes - and I want to return these raw bytes. I don’t know how he is trying to decode it in unicode - apparently not using utf8 , because zlib could not decompress the data. (He could not unpack it, even when I did not try to execute .encode("utf8") before zlibbing-it, it was just a test.)

How to make django not unify POST variables? Or, if so, how can I cancel it?

+4
source share
1 answer

You can undo it.

Try using * smart_str * from django.utils.encoding :

 from django.utils.encoding import smart_str event_dicts_td_json_gz = smart_str( event_dicts_td_json_gz ) 

Check out the docs here, please: https://docs.djangoproject.com/en/dev/ref/unicode/#useful-utility-functions

0
source

Source: https://habr.com/ru/post/1387820/


All Articles