Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to deal with ImmutableMultiDict

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

1 comments
  1. 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.

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading