3

I tried sending a get request on Advanced REST Client through this URL:

http://127.0.0.1:8000/report/game/?gamecategory=电子游艺

However an error returned:

Exception happened during processing of request from ('127.0.0.1', 51260)
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 599, in process_request_thread
    self.finish_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Users/deanchristianarmada/Desktop/projects/jaguar/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 99, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 655, in __init__
    self.handle()
  File "/Users/deanchristianarmada/Desktop/projects/jaguar/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 176, in handle
    self.rfile, self.wfile, self.get_stderr(), self.get_environ()
  File "/Users/deanchristianarmada/Desktop/projects/jaguar/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 150, in get_environ
    if '?' in path:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 27: ordinal not in range(128)

It's not it in the code as this is my only code:

class ReportGame(APIView):
    '''
    @brief      Game Report on Lion's new page
    '''

    permission_classes = (AllowAny, )
    model = GameAccount
    serializer = ReportGameSerializer

    def get(self, request, *args, **kwargs):
        '''
        '''

        print 'dean'
        return Response(data='data', status=status.HTTP_200_OK)

Is there a way to resolve this using my current URL Get parameters?

I'm using:

python == 2.7.10
django-rest-framework == 3.5
django == 1.10

UPDATE

This is very weird as I've tried to do the same request using python's library requests and it actually works fine

>>> url = 'http://127.0.0.1:8000/report/game/?gamecategory=电子游艺'
>>> requests.get(url)
<Response [200]>
>>> x.content
'{"members":[{"id":5,"member":"chrisguinto","bet_record_count":3,"valid_bet_amount":6.0,"bet_amount":7.0,"profit":0.0},{"id":6,"member":"deanchrisarmada","bet_record_count":2,"valid_bet_amount":8.0,"bet_amount":6.0,"profit":2.0}],"member_count":2}'

I also tried it sending a request with AngularJS and it worked:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<p>Today's welcome message is:</p>

<h1>{{myWelcome}}</h1>

</div>

<p>The $http service requests a page on the server, and the response is set as the value of the "myWelcome" variable.</p>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $http.get("http://127.0.0.1:8000/report/game/?gamecategory=电子游艺")
  .then(function(response) {
      $scope.myWelcome = response.data;
  });
});
</script>

</body>
</html>

**CONCLUSION: ** I guess it's just Advanced Rest Client's end

Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116
  • We need to know the exact byte sequence that your browser sent to the server. The easiest way to get this is with Wireshark or similar; you may also be able to dig it out of the browser's debugger's network pane. Also, I don't know from Django, but if you *can* upgrade the server to Python 3, that may make this problem go away all by itself; one of the big selling points of v3 is much improved support for Unicode. – zwol Nov 24 '16 at 03:03
  • Python have to encode unicode to bytes before it can send it It seems Python doesn't know what encoding to use so it try to use `ascii`. But you can encode on your own using `utf-8` or other encoding. HTTP use special method to send native chars - it send it as hex code with % at start. – furas Nov 24 '16 at 03:30
  • BTW: you can use your URL in web browser (Chrome/FIrefox) and use built-in DevTool to see request send from browser to server. This way you can see how browser encode Chinese characters. – furas Nov 24 '16 at 03:33
  • my browser use `gamecategory=%E7%94%B5%E5%AD%90%E6%B8%B8%E8%89%BA` – furas Nov 24 '16 at 03:45
  • `import urllib ; print urllib.urlencode({'gamecategory':'电子游艺'})` – furas Nov 24 '16 at 03:47
  • BTW: always show full error message (Traceback), not only last message. There can be more usefull information. – furas Nov 24 '16 at 03:48

0 Answers0