Reputation: 1275
I have a very simple installation of Django REST with a single table with 4 fields in it. I can interact with the API without any issues, but when I do a GET call the JSON I get back has these mystery 5 characters at the beginning that is breaking the JSON.
djangorestframework (2.3.14) Python (2.7)
models.py
class SensorData(models.Model):
string = models.CharField(max_length=255)
sensordata = models.CharField(max_length=255, null=True)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'sensor_data'
serialzers.py
from rest_framework import serializers
from models import SensorData
class SensorDataSerializer(serializers.ModelSerializer):
"""
Serializer for test API endpoint
"""
timestamp = serializers.DateTimeField(format='%Y-%m-%d')
class Meta:
model = SensorData
fields = ('id', 'string', 'sensordata', 'timestamp')
views.py
from rest_framework import viewsets
from serializers import SensorDataSerializer
from models import SensorData
class SensorDataViewSet(viewsets.ModelViewSet):
"""
API endpoint for test sensor data
"""
queryset = SensorData.objects.all()
serializer_class = SensorDataSerializer
settings.py
REST_FRAMEWORK = {
# Use hyperlinked styles by default.
# Only used if the `serializer_class` attribute is not set on a view.
'DEFAULT_MODEL_SERIALIZER_CLASS':
'rest_framework.serializers.ModelSerializer',
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
),
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
)
}
My Response:
)]}',
[{"id": 1, "string": "Jassen", "sensordata": "test", "timestamp": "2014-07-15"}]
Upvotes: 1
Views: 203
Reputation: 1275
I found the root cause which was Djangular, which has a middleware that was appending the bad characters to the beginning of the JSON.
'djangular.middleware.AngularJsonVulnerabilityMiddleware'
Hopefully that saves someone some time.
Upvotes: 2