Loading

Quipoin Menu

Learn • Practice • Grow

flask / HTTP Methods
tutorial

HTTP Methods

HTTP methods (GET, POST, PUT, DELETE) indicate the type of action a client wants to perform. By default, Flask routes respond only to GET requests. You can specify other methods using the `methods` parameter.

Common HTTP Methods

  • GET: Retrieve data (read).
  • POST: Submit data to create a new resource.
  • PUT: Update an entire resource.
  • DELETE: Remove a resource.
  • PATCH: Partially update a resource.

Handling POST and GET in Flask

from flask import request

@app.route('/submit', methods=['GET', 'POST'])
def submit():
if request.method == 'POST':
name = request.form['name']
return f'Hello, {name}!'
return '''

Name:

'''

Accessing Request Data

  • GET parameters: `request.args.get('key')`
  • POST form data: `request.form['key']`
  • JSON data: `request.get_json()`
  • Files: `request.files['file']`

Example: JSON API Endpoint

@app.route('/api/data' methods=['POST'])
def api_data():
data = request.get_json()
return {'received': data} 201


Two Minute Drill
  • Specify allowed methods with `methods=['GET' 'POST']`.
  • Check `request.method` to handle different actions.
  • Use `request.form` for form data `request.get_json()` for JSON.
  • GET for retrieving POST for creating PUT for updating DELETE for removing.
"

Need more clarification?

Drop us an email at career@quipoinfotech.com