How to document the post body using flask-ReSTplus?

10,288

I have solved it (partially) using the following model

""" Model for documenting the API"""

insert_user_data = ns_database.model(
    "Insert_user_data",
    {
        "customer_id": fields.String(description="cust ID", required=True),
        "service_id": fields.String(description="service ID", required=True),
        "customer_name": fields.String(description="Customer1", required=True),
        "site_name": fields.String(description="site", required=True),
        "service_type": fields.String(description="service", required=True),
    },
)


@ns_database.route("/insert_user")
class database(Resource):
    @ns_database.expect(insert_user_data)
    def post(self):
        """insert data"""
        json_data = request.json
        customer_id = json_data["customer_id"]
        service_id = json_data["service_id"]
        customer_name = json_data["customer_name"]
        site_name = json_data["site_name"]
        service_type = json_data["service_type"]

now the API shows model for data input and an example

solved

Share:
10,288
Prathisrihas Reddy
Author by

Prathisrihas Reddy

enter code here

Updated on August 05, 2022

Comments

  • Prathisrihas Reddy
    Prathisrihas Reddy almost 2 years

    insert user data

    How to document the input body that is expected to be posted in the value field to appear so that the user knows what to post? the following data is used currently:

    {
     "customer_id": "",
     "service_id": "",
     "customer_name": "",
     "site_name": "",
     "service_type": ""
    }
    

    can we populate the value by default with the above json?

    Code:

    post_parser = reqparse.RequestParser()
    post_parser.add_argument('database',  type=list, help='user data', location='json')
    
    @ns_database.route('/insert_user')
    class database(Resource):
    @ns_database.expect(post_parser)
    def post(self):
        """insert data"""
        json_data = request.json
        customer_id = json_data['customer_id']
        service_id = json_data['service_id']
        customer_name = json_data['customer_name']
        site_name = json_data['site_name']
        service_type = json_data['service_type']
    
  • Prathisrihas Reddy
    Prathisrihas Reddy almost 6 years
    The intention is to build an API but not rendering web pages. The above image is a part of the swagger documentation for the same API
  • Ahmouse
    Ahmouse almost 6 years
    Alright, but then I'm confused as to what you are asking
  • dmulter
    dmulter almost 6 years
    I believe if you get rid of the RequestParser.add_argument code that you aren't showing above, then Swagger UI will generate input fields for each of your API model fields.
  • Prathisrihas Reddy
    Prathisrihas Reddy almost 6 years
    @dmulter even if there was no model and without @api.expect(model) just removing the RequestParser.add_argument will solve it is what you mean or?
  • Prathisrihas Reddy
    Prathisrihas Reddy almost 6 years
    Like the answer I have written, I am expecting
  • dmulter
    dmulter almost 6 years
    I meant that you only need the model definition along with @api.expect(model) to generate correct Swagger UI. You should not need the RequestParser.add_argument code. That's what is creating the payload parameter in the UI.
  • Prathisrihas Reddy
    Prathisrihas Reddy almost 6 years
    Yeah, I realised that and posted the answer :) thank you