django rest framework - How to add post parameters to api document(drf_yasg)?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







9















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?










share|improve this question




















  • 1





    The in_=openapi.IN_FORM tells it that you expect it to be a form, while your API is configured to accept JSON?

    – Tarun Lalwani
    Jun 22 '18 at 7:08






  • 1





    @SungHoKim based on the JSON response, the server seems to be looking for valid JSON data in the POST body. Did you try formatting your request body accordingly? Maybe like {'x': 124, 'y': 124124} ?

    – Ralf
    Jun 25 '18 at 12:09






  • 1





    Try changing your openapi.IN_FORM to openapi.IN_BODY

    – Helder Sepulveda
    Jun 25 '18 at 21:02


















9















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?










share|improve this question




















  • 1





    The in_=openapi.IN_FORM tells it that you expect it to be a form, while your API is configured to accept JSON?

    – Tarun Lalwani
    Jun 22 '18 at 7:08






  • 1





    @SungHoKim based on the JSON response, the server seems to be looking for valid JSON data in the POST body. Did you try formatting your request body accordingly? Maybe like {'x': 124, 'y': 124124} ?

    – Ralf
    Jun 25 '18 at 12:09






  • 1





    Try changing your openapi.IN_FORM to openapi.IN_BODY

    – Helder Sepulveda
    Jun 25 '18 at 21:02














9












9








9








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?










share|improve this question
















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?







django django-rest-framework swagger drf-yasg






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 16:10









Ghada

711315




711315










asked Jun 19 '18 at 12:59









SungHo KimSungHo Kim

964




964








  • 1





    The in_=openapi.IN_FORM tells it that you expect it to be a form, while your API is configured to accept JSON?

    – Tarun Lalwani
    Jun 22 '18 at 7:08






  • 1





    @SungHoKim based on the JSON response, the server seems to be looking for valid JSON data in the POST body. Did you try formatting your request body accordingly? Maybe like {'x': 124, 'y': 124124} ?

    – Ralf
    Jun 25 '18 at 12:09






  • 1





    Try changing your openapi.IN_FORM to openapi.IN_BODY

    – Helder Sepulveda
    Jun 25 '18 at 21:02














  • 1





    The in_=openapi.IN_FORM tells it that you expect it to be a form, while your API is configured to accept JSON?

    – Tarun Lalwani
    Jun 22 '18 at 7:08






  • 1





    @SungHoKim based on the JSON response, the server seems to be looking for valid JSON data in the POST body. Did you try formatting your request body accordingly? Maybe like {'x': 124, 'y': 124124} ?

    – Ralf
    Jun 25 '18 at 12:09






  • 1





    Try changing your openapi.IN_FORM to openapi.IN_BODY

    – Helder Sepulveda
    Jun 25 '18 at 21:02








1




1





The in_=openapi.IN_FORM tells it that you expect it to be a form, while your API is configured to accept JSON?

– Tarun Lalwani
Jun 22 '18 at 7:08





The in_=openapi.IN_FORM tells it that you expect it to be a form, while your API is configured to accept JSON?

– Tarun Lalwani
Jun 22 '18 at 7:08




1




1





@SungHoKim based on the JSON response, the server seems to be looking for valid JSON data in the POST body. Did you try formatting your request body accordingly? Maybe like {'x': 124, 'y': 124124} ?

– Ralf
Jun 25 '18 at 12:09





@SungHoKim based on the JSON response, the server seems to be looking for valid JSON data in the POST body. Did you try formatting your request body accordingly? Maybe like {'x': 124, 'y': 124124} ?

– Ralf
Jun 25 '18 at 12:09




1




1





Try changing your openapi.IN_FORM to openapi.IN_BODY

– Helder Sepulveda
Jun 25 '18 at 21:02





Try changing your openapi.IN_FORM to openapi.IN_BODY

– Helder Sepulveda
Jun 25 '18 at 21:02












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50929098%2fdjango-rest-framework-how-to-add-post-parameters-to-api-documentdrf-yasg%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 22 '18 at 18:15









        axnsanaxnsan

        342310




        342310
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50929098%2fdjango-rest-framework-how-to-add-post-parameters-to-api-documentdrf-yasg%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings