Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest-framework api and unicode characters

I am using django-rest-framework to create an api for my application. My application uses greek letters as values of its models. I have created my viewsets and used UnicodeJSONRenderer to return the json result.

class ChapterViewSet(viewsets.ModelViewSet):
    queryset = Chapter.objects.all()
    serializer_class = ChapterSerializer
    renderer_classes = (UnicodeJSONRenderer, )

Json is returned but the greek letters are not recognized by the browser("Ξ ΟΟΟΞΈΞ΅ΟΞ·). On chrome's dev console though on network tab the preview of the response shows greek letters normally. How can I make my browser recognize greek letters?

like image 556
Apostolos Avatar asked Oct 10 '14 12:10

Apostolos


People also ask

Is Django GOOD FOR REST API?

Django REST framework (DRF) is a powerful and flexible toolkit for building Web APIs. Its main benefit is that it makes serialization much easier. Django REST framework is based on Django's class-based views, so it's an excellent option if you're familiar with Django.

Does Django automatically encode user input?

All of Django's database backends automatically convert strings into the appropriate encoding for talking to the database. They also automatically convert strings retrieved from the database into strings. You don't even need to tell Django what encoding your database uses: that is handled transparently.

What is the difference between Django and REST API?

REST is the general framework, while Django Rest Framework DRF is a specific REST framework used in Django. It's easy to make Web APIs with Django because of its modular and customizable armature. Django emphasizes security by furnishing defense against common SQL injection and cross-site request forgery attacks.


2 Answers

It's a browser issue.

UTF-8 is the default encoding for JSON content; Django Rest Framework is encoding your JSON properly into UTF-8, but your browser is not displaying it correctly.

Browsers would display it correctly if provided with a charset=utf-8 in the Content-Type HTTP Header. However, the spec defines another way of determining the encoding, so this should not be used. Django Rest Framework honours this by not including it.

There is an open ticket for Chrome on this, but unfortunately nobody seems to care. Other browsers seem to have the same problem. See also this SO question.

like image 153
e18r Avatar answered Oct 02 '22 00:10

e18r


This is a really weird issue that I ran into too; my first impression was that Django REST Framework was supposed to set the charset to UTF-8 in the Content-Type header, but this has already been filed as issue #2891 and there is apparently a lot of contention over it.

The fix I ended up using is simply to set the UNICODE_JSON setting to False. This results in larger responses, especially if you have lots of unicode characters in your response, as e.g. a horizontal ellipsis becomes \u2026 in a string rather than its equivalent 3-byte UTF-8 representation, but it's less likely to be misunderstood by clients.

like image 36
Atul Varma Avatar answered Oct 01 '22 22:10

Atul Varma