I’m trying to test my flask API with a POST
request, but I have problems to deal with the ImmutableMultiDict.
The API:
JavaScript
x
5
1
@app.route("/df/", methods = ['GET', 'POST'])
2
def get_df():
3
if request.method == 'POST':
4
select = request.form
5
request:
JavaScript
1
2
1
curl -X POST http://127.0.0.1:5000/df/ -d '{"ticker": "ETH-PERP"}'
2
print(select)
:
JavaScript
1
2
1
ImmutableMultiDict([('{"ticker": "ETH-PERP"}', '')])
2
How do I access the value (“ETH-PERP”) ?
I tried:
JavaScript
1
2
1
select = request.form.getlist("ticker")
2
output: []
JavaScript
1
3
1
select = request.form.to_dict(flat=True)
2
print(select[0])
3
output: keyError: 0
JavaScript
1
2
1
select = request.form.to_dict().values()[0]
2
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.