0

created an api and added swagger to the api with the help of the package

drf-yasg

the current updated version 1.20.0, then added code like this

success_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='200'), 'success': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})
    
error_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='400'), 'error': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})

class TestView(APIView):
    api_view = ['POST']
    authentication_classes = [SessionAuthentication, TokenAuthentication]

    invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)

    @swagger_auto_schema(
        manual_parameters=[invitation_file], operation_description="description",
        responses={200: success_res_data, 400: error_res_data}
    )
    def post(self, request):
        invitation_file = request.data.get('invitation_file', None)

    invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)

    @swagger_auto_schema(
        manual_parameters=[invitation_file], operation_description="description",
        responses={200: success_res_data, 400: error_res_data}
    )
    def post(self, request):
        invitation_file = request.data.get('invitation_file', None)

this invitation_file variable is returning None even if we pass the file from front-end

Avin Mathew
  • 336
  • 11
  • 25
  • Are you **certain** this code is intended for Python 2.2? Which was released about 20 years ago? Are you aware that 3.9 is mature, 3.10 has been released and is actively being maintained, 3.11 has just come out of beta as I write this, and 3.12 is in development? – Karl Knechtel Aug 14 '22 at 18:41
  • my mistake, changed from python 2.2 to 3.6, thanks karl – Avin Mathew Aug 17 '22 at 06:55

1 Answers1

0

after a little research and checking the same api in postman, changed the code from whats above to

success_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='200'), 'success': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})
    
error_res_data = openapi.Schema(type=openapi.TYPE_OBJECT, properties={'status': openapi.Schema(type=openapi.TYPE_NUMBER, title='400'), 'error': openapi.Schema(type=openapi.TYPE_OBJECT, properties={'message_header': openapi.Schema(type=openapi.TYPE_STRING), 'message': openapi.Schema(type=openapi.TYPE_STRING)})})

class TestView(APIView):
    api_view = ['POST']
    authentication_classes = [SessionAuthentication, TokenAuthentication]

    invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)


    @swagger_auto_schema(
            manual_parameters=[invitation_file],operation_description="API Description", consumes="multipart/form-data",
        responses={200: success_res_data, 400: error_res_data}
    )
    def post(self, request):
        invitation_file = request.data.get('invitation_file', None)

    invitation_file = openapi.Parameter('invitation_file', openapi.IN_QUERY, type=openapi.TYPE_FILE, required=True)

    @swagger_auto_schema(
        manual_parameters=[invitation_file], operation_description="description",
        responses={200: success_res_data, 400: error_res_data}
    )
    def post(self, request):
        invitation_file = request.data.get('invitation_file', None)

now click on the unlock button on the right of the api and add the "Token auth-token" and click authenticate

now after calling the api and using pdb the value of the passed file is shown for the variable invitation_file

Avin Mathew
  • 336
  • 11
  • 25