main.py
import json
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST'])
def index():
mimetype = request.mimetype
if mimetype == 'application/x-www-form-urlencoded':
form = json.loads(next(iter(request.form.keys())))
elif mimetype == 'multipart/form-data':
form = dict(request.form)
elif mimetype == 'application/json':
form = request.json
else:
form = request.data.decode()
print(mimetype, form, type(form))
return form
if __name__ == '__main__':
app.run()
Test
curl -X POST http://127.0.0.1:5000/ --data "{\"a\":1, \"b\":2}"
curl -X POST http://127.0.0.1:5000/ -F a=1 -F b=2
curl -X POST -H "Content-type: application/json" http://127.0.0.1:5000/ --data "{\"a\":1, \"b\":2}"
Result
application/x-www-form-urlencoded {'a': 1, 'b': 2} <class 'dict'>
multipart/form-data {'a': '1', 'b': '2'} <class 'dict'>
application/json {'a': 1, 'b': 2} <class 'dict'>
flask.request.data.decode("utf-8")
blank, but I thought it was a client side problem. Thanks for confirming flask is doing this! Wow omg 10 years later! this conversation really saved my biscuit today ! I was puzzled for a few hours. – Staid