1import flask
2app = flask.Flask('your_flask_env')
3
4@app.route('/register', methods=['GET', 'POST'])
5def register():
6 if flask.request.method == 'POST':
7 username = flask.request.values.get('user') # Your form's
8 password = flask.request.values.get('pass') # input names
9 your_register_routine(username, password)
10 else:
11 # You probably don't have args at this route with GET
12 # method, but if you do, you can access them like so:
13 yourarg = flask.request.args.get('argname')
14 your_register_template_rendering(yourarg)
15
1from flask import Flask, request, jsonify
2
3app = Flask(__name__)
4
5@app.route('/foo', methods=['POST'])
6def foo():
7 data = request.json
8 return jsonify(data)
1The docs describe the attributes available on the request. In most common cases request.data will be empty because it's used as a fallback:
2
3request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle.
4
5request.args: the key/value pairs in the URL query string
6request.form: the key/value pairs in the body, from a HTML post form, or JavaScript request that isn't JSON encoded
7request.files: the files in the body, which Flask keeps separate from form. HTML forms must use enctype=multipart/form-data or files will not be uploaded.
8request.values: combined args and form, preferring args if keys overlap
9request.json: parsed JSON data. The request must have the application/json content type, or use request.get_json(force=True) to ignore the content type.
10All of these are MultiDict instances (except for json). You can access values using:
11
12request.form['name']: use indexing if you know the key exists
13request.form.get('name'): use get if the key might not exist
14request.form.getlist('name'): use getlist if the key is sent multiple times and you want a list of values. get only returns the first value.