Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this django-rest-swagger API documentation display/work properly?

I've built a Django API that when given an email address via POST will respond with a boolean value indicating weather or not that email address already exists in my database:

class isEmailTaken(views.APIView):
    permission_classes = [permissions.AllowAny,]

    def post(self, request, *args, **kwargs):
        try:
            email = request.DATA['email']
        except KeyError:
            return HttpResponse(
                'An email was not given with this request.',
                status=status.HTTP_400_BAD_REQUEST,
            )
        return HttpResponse(
            json.dumps(
                User.objects.filter(email=email),
                content_type="application/json",
                status=status.HTTP_200_OK,
            )
        )

Now I would like to use the django-rest-swagger package to automatically generate documentation for this API. I installed the package and inserted the comments you see above between the triple-quotes. When I look at the documentation produced by django-rest-swagger for this API, I see the image below.

enter image description here

However, when I click the Try it out! button, I get the error shown below. Notably, it never gives me a chance to input the email argument that it should send via POST.

enter image description here

Why doesn't the Django-Swagger-Package create docs that allow me to properly the argument "email" via POST? How do I make this work?

like image 533
Saqib Ali Avatar asked Sep 09 '14 20:09

Saqib Ali


People also ask

Is REST API documentation template available on Swagger?

Use Swagger Inspector to quickly generate your OAS-based documentation for existing REST APIs by calling each end point and using the associated response to generate OAS-compliant documentation, or string together a series of calls to generate a full OAS document for multiple API endpoints.


2 Answers

I tested this with cigar_example which is made by django-rest-swagger and in that example they written one custom view which is also not rendering input parameters

Lastly i look into source code and found that django-rest-swagger needs get_serializer_class to build body parameters

So it worked with the following code:

class isEmailTaken(views.APIView):
    permission_classes = [permissions.AllowAny,]
    serializer_class = IsEmailTakenSerializer

    def get_serializer_class(self):
        return self.serializer_class

    def post(self, request, *args, **kwargs):
        try:
            email = request.DATA['email']
        except KeyError:
            return HttpResponse(
               'An email was not given with this request.', 
                status=status.HTTP_400_BAD_REQUEST,
            )
        return HttpResponse(
            json.dumps(
                User.objects.filter(email=email), 
                content_type="application/json",
                status=status.HTTP_200_OK,
             )
         )

and IsEmailTakenSerializer:

from rest_framework import serializers


class IsEmailTakenSerializer(serializers.Serializer):
    email = serializers.EmailField()
like image 79
Venkatesh Bachu Avatar answered Sep 20 '22 08:09

Venkatesh Bachu


django-rest-swagger tries to send a POST request without some data.

First you have to fix your view like this:

from rest_framework import status
from django.http import HttpResponse
import json

def post(self, request, *args, **kwargs):
    try:
        email = request.DATA['email']
    except KeyError:
        return HttpResponse(
            'An email was not given with this request.', 
            status=status.HTTP_400_BAD_REQUEST,
        )
    return HttpResponse(
        json.dumps(
            User.objects.filter(email=email), 
            content_type="application/json",
            status=status.HTTP_200_OK,
        )
    )

If you try this, you should see your nice error message now.

Next step is to look at django-rest-swagger docs to find out what to do that it renders a html form field right above the "Try it out" button.

like image 32
Norman8054 Avatar answered Sep 17 '22 08:09

Norman8054