I’m trying to test my flask API with a POST request, but I have problems to deal with the ImmutableMultiDict.
The API:
@app.route("/df/", methods = ['GET', 'POST'])
def get_df():
if request.method == 'POST':
select = request.form
request:
curl -X POST http://127.0.0.1:5000/df/ -d '{"ticker": "ETH-PERP"}'
print(select):
ImmutableMultiDict([('{"ticker": "ETH-PERP"}', '')])
How do I access the value ("ETH-PERP") ?
I tried:
select = request.form.getlist("ticker")
output: []
select = request.form.to_dict(flat=True)
print(select[0])
output: keyError: 0
select = request.form.to_dict().values()[0]
output: 'dict_values' object is not subscriptable
any suggestions?
>Solution :
The problem is that the request data is not a form, it’s JSON.
You should probably use request.json to read it, which should give you a regular dictionary.
tbh who so ever gave the above solution I’m sure he is not a software dev or has never written a single line of code in his life. Such an idiot, if the request is present in request. form how can someone fetch it from request.json.