how to pass post parameter in Django rest swagger?

1.2k Views Asked by At

I have integrated swagger with django rest framework, but the swagger docs does not create a input box to post data for post request.

I am using Function based views with decorator(@api_view)

@api_view(['GET','POST'])
@permission_classes((AllowAny, ))
@renderer_classes([OpenAPIRenderer, SwaggerUIRenderer])
def preferences(request,format=None):
    try:
             "logic starts here "

in urls.py i have added:

  schema_view = get_swagger_view(title='API')         
  path('', schema_view, name="schema_view"),

in settings.py :

SWAGGER_SETTINGS = {
'USE_SESSION_AUTH': False,
'JSON_EDITOR': True,
'api_version': '0.1',
'enabled_methods': [
    'GET',
    'POST',
],
'SECURITY_DEFINITIONS': {
    "api_key": {
        "type": "apiKey",
        "name": "Authorization",
        "in": "header"
    },
},

}

But, when i open the api url i'm getting something like this (in image) where i'm unable to pass the parameters through postscreenshot of api hit url

what can be the problem here?is there any other changes to be done ?

2

There are 2 best solutions below

1
Ken4scholars On

When you click on the Try it Out link, you should get an input to fill in the POST data

0
Nandu On

make sure the class is imported from the generics API, example:

from rest_framework import generics

class Something (generics.GenericAPIView):

     serializer_class=SomeSerializer

     def post(self,request):
         pass