-1

Is there a way to give a GET route default params in python Flask API?

for example: I have this route: GET user?fields=... and I can pass params with fields that I want to get from this model (username, email and etc) and if I don't pass this fields param it will get a default fields for the user that I will define in the route.

EDIT:: and is there a way to do it in POST requests

Dkova
  • 1,087
  • 4
  • 16
  • 28

1 Answers1

0
@app.route("/user", defaults={'fields': your_default_value})
@user.route('/user/<fields>')
def user(fields): # it will take this default is don't pass
                                     # parameter

If you will not pass the fields parameter it will take your default value.

Solution 2:

If you are submiting your form with get method you will have a request object in your action method

request.args.get('id') # here you can replace id with your field, 
                       # using this you can access all your fields

If you are submitting your form with post request you can do following

request.form['code'] #this how you will get access to your form fields in post method
                     #you can change code with your form field
  • what is @user.route('/user/') ? and i don't what to pass it in the URL I want to pass it as params only – Dkova Jan 02 '18 at 08:24
  • Are you submitting a form with get or post method? if you arem, then you can use the above function with your field name of form – Harsh Mehta Jan 02 '18 at 08:32
  • if I send them with post and JSON format? – Dkova Jan 02 '18 at 08:47
  • then you can do the following: jsonRes = request.get_json() and then to access the fields you can do it by jsonRes['you_field_name'] for that you have to import json from flask – Harsh Mehta Jan 02 '18 at 08:52