-
-
Save fluential/723e2ffe47683659f4e92958d65b281b to your computer and use it in GitHub Desktop.
Obtaining all data (keys/values) from POST request (form) in Python Flask.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" Obtaining all form fields (dynamically) from a POST request. | |
$ curl http://127.0.0.1:8000/post -d "name=Nemo&country=Norway" | |
$ curl http://127.0.0.1:8000/data -d "company=Axos Inc.&business=IT Outsourcing" | |
""" | |
from flask import abort, Flask, jsonify, request | |
__author__ = "@ivanleoncz" | |
app = Flask(__name__) | |
@app.route('/data', methods=['POST']) | |
def f_data(): | |
if request.method == "POST": | |
print(request.headers) | |
fields = [k for k in request.form] | |
values = [request.form[k] for k in request.form] | |
data = dict(zip(fields, values)) | |
return jsonify(data) | |
else: | |
abort(405) # aborting: Method Not Allowed | |
if __name__ == "__main__": | |
app.run(host="127.0.0.1", port=8000, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment