How to document the post body using flask-ReSTplus?

孤人 提交于 2020-06-24 11:53:05

问题


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']

回答1:


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




回答2:


Assuming you are using a Flask template to return the /database/insert_user webpage, you could simply make the variable containing database information (customer_id, etc) accessible to where render_template is called, then pass the variable on to it.

For example if you wanted to pass the customer_id variable:

return render_template("insert_user.html", 
x=customer_id)

assuming insert_user.html is your template file you can then place the data where ever you want it using {{ x }}



来源:https://stackoverflow.com/questions/51039018/how-to-document-the-post-body-using-flask-restplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!