Django Rest Framework custom POST URL endpoints with defined parameter (request.POST) with Swagger or other doc
As you said django-rest-swagger is deprecated.
That's why it is recommended to use drf-yasg.
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
class ArticleViewSet(viewsets.ModelViewSet):
@swagger_auto_schema(request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'test_token': openapi.Schema(type=openapi.TYPE_STRING, description='string'),
}
))
def create(self, request, *args, **kwargs):
...
Or if you want to use a DRF action
@swagger_auto_schema(method="post", request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'test_token': openapi.Schema(type=openapi.TYPE_STRING, description='string'),
}
))
@action(method=["post"], detail=False)
def my_post_action(self, request, *args, **kwargs):
...
Or with api view:
# here we define that this view accepts a json (or object parameter) that has test_token parameter inside of it
@swagger_auto_schema(method='post',
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT, # object because the data is in json format
properties={
'test_token': openapi.Schema(type=openapi.TYPE_STRING, description='this test_token is used for...'),
}
), operation_id="token_view")
# your view
@api_view(['POST'])
def token_view(request):
pass
And your url.py will look like so
# define some basic info about your api for swagger
schema_view = get_schema_view(
openapi.Info(
title="Snippets API",
default_version='v1',
description="Test description",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="[email protected]"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=[permissions.AllowAny],
)
urlpatterns = [
# define your api view url
path('token_view/', token_view),
# define the url of the swagger ui
url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
]