9

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

2 Answers2

9

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

BWStearns
  • 2,147
  • 2
  • 17
  • 28
  • 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. – dmulter Jun 27 '18 at 18:14
  • @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 Jun 28 '18 at 08:45
  • 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. – dmulter Jun 28 '18 at 14:55
  • Yeah, I realised that and posted the answer :) thank you – Prathisrihas Reddy Jun 28 '18 at 19:39
-1

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 }}

Ahmouse
  • 174
  • 1
  • 9