Remove unicode char in a POST body

时间:2016-05-02 21:40:13

标签: json django unicode django-rest-framework

Using Django Rest Framework, a JSON containing the unicode char \x0b makes a 400 bad request error.

I'm trying to change my request data to remove this unicode char:

 def initialize_request(self, request, *args, **kwargs):    
         data = request.body
         new_data = data.decode('utf-8')
         new_data = new_data.replace('\x0b', '')
         request.body = new_data.encode('utf-8')

         r = super(ScanListCreateAPIView, self).initialize_request(request, *args, **kwargs)

         return r

but this returns a "can't set attribute" error.

Is there a way to alter the 'body' attribute? Or to clone the old request to a new request?

0 个答案:

没有答案