Is there a way to get the value of a nested dict in an Immutabledict sent by request werkzeug (bulb)?

I asked a question in the past, but still run into a problem. address_dict = {'address': {'US': 'San Francisco', 'US': 'New York', 'UK': 'London'}}

When the above parameters were sent via requests, how can I get the values ​​in the address key using request.form on Flask?

import requests url = 'http://example.com' params = {"address": {"US": "San Francisco", "UK": "London", "CH": "Shanghai"}} requests.post(url, data=params) 

Then I got this in the context of flask.request.

 ImmutableMultiDict([('address', u'US'), ('address', 'US'), ('address', 'UK')]) 

How can I get the value in each address key?

Thanks.

+6
source share
1 answer

You submit a complex nested data structure as an HTML form; it will not work as you expect. Encode it as JSON:

 import json import requests url = 'http://example.com/' payload = {"address": {"US": "San Francisco", "UK": "London", "CH": "Shanghai"}} data = json.dumps(payload) headers = {'Content-Type': 'application/json'} requests.post(url, data=data, headers=headers) 

In your Flask application, it will be available through request.json (already decoded).

+12
source

All Articles