x_param = openapi.Parameter('x', in_=openapi.IN_FORM, description='srring', type=openapi.TYPE_STRING) y_param = openapi.Parameter('y', in_=openapi.IN_FORM, description='string', type=openapi.TYPE_STRING) @swagger_auto_schema(method='post', manual_parameters=[x_param,y_param]) @api_view(['POST']) def test(request): pass
I used drf_yasg
as a swagger.
I did the coding above and tested it with swagger, and when I checked with Chrome, the request payload is x = 124 & y = 124124
.
And, with the following message, a bad request error occurred.
{ "detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)" }
Is it wrong to add post parameters to the swagger?
The @swagger_auto_schema decorator. You can use the @swagger_auto_schema decorator on view functions to override some properties of the generated Operation . For example, in a ViewSet , from drf_yasg.
Django REST framework introduces a Request object that extends the regular HttpRequest, this new object type has request. data to access JSON data for 'POST', 'PUT' and 'PATCH' requests. However, I can get the same data by accessing request. body parameter which was part of original Django HttpRequest type object.
The problem is that you are adding parameters of type form
to swagger, but your view seems to expect a json payload in the request body. In that case you probably want to use request_body
with an openapi.Schema
object.
@swagger_auto_schema(method='post', request_body=openapi.Schema( type=openapi.TYPE_OBJECT, properties={ 'x': openapi.Schema(type=openapi.TYPE_STRING, description='string'), 'y': openapi.Schema(type=openapi.TYPE_STRING, description='string'), } )) @api_view(['POST']) def test(request): pass
This will automatically wrap your Schema
into a Parameter
of in_=openapi.IN_BODY
. See https://drf-yasg.readthedocs.io/en/stable/openapi.html for details.
Of course, the preferred way would be to use a class-based GenericAPIView
together with a Serializer
, which would simplify both the view code and the doc introspection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With