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?
Advertisement
Answer
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.