4

I created a Django Api. I used rest_framework.generics.CreateAPIView to post. It works well in default browser. But When i use Postman it throws a error.

views.py

class AuthorCreateAPIView(CreateAPIView):
  queryset = Author.objects.all()
  serializer_class = AuthorCreateUpdateSerializer

serializers.py

class AuthorCreateUpdateSerializer(ModelSerializer):
  class Meta:
  model = Author
  fields = [
    'name',
    'biography',
  ]

Error : "detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)"

Postman view

Sadman Sobhan
  • 516
  • 10
  • 27

1 Answers1

7

The problem is that you used form-data as your input, you should use application/json instead.

however you can fix your project's settings file to accept form-data too. You need to add FormParser to your DEFAULT_PARSER_CLASSES in your project's settings.py. It should look like this:

REST_FRAMEWORK = {
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',
        'rest_framework.parsers.FormParser',
    )
}
Navid777
  • 3,591
  • 8
  • 41
  • 69